Discussion:
select and spaces in end of field
(too old to reply)
Olivier B.
2012-12-12 19:15:53 UTC
Permalink
hello,

one table:

[name]
'toto'
'toto '
'toto '



i shearch the syntax for select only toto with the good number of
space after

using WHERE [name]= 'toto'
return:
'toto'
'toto '
'toto '

using WHERE [name] LIKE 'toto ' return
'toto '
'toto '

Thanks for any help.
--
pas de turlututu. apres l'@robase
Erland Sommarskog
2012-12-12 19:44:17 UTC
Permalink
Post by Olivier B.
[name]
'toto'
'toto '
'toto '
i shearch the syntax for select only toto with the good number of
space after
using WHERE [name]= 'toto'
'toto'
'toto '
'toto '
using WHERE [name] LIKE 'toto ' return
'toto '
'toto '
Assuming that your search string is an variable, you could do:

WHERE name = @value
AND datalength(name) = datalength(@value)

Be careful that column and variable are of the same type, that is
either both should be varchar or both should be nvarchar.
--
Erland Sommarskog, SQL Server MVP, ***@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
Olivier B.
2012-12-12 22:24:56 UTC
Permalink
On Wed, 12 Dec 2012 20:44:17 +0100, Erland Sommarskog
Post by Erland Sommarskog
Be careful that column and variable are of the same type, that is
either both should be varchar or both should be nvarchar.
thank you, this bypass my problem, however calculate the length in
request is an additional load for my SQL server, there is no way to
compare strictly spaces?

i forgot: sql express 2012

thanks a lot
--
pas de turlututu. apres l'@robase
Erland Sommarskog
2012-12-12 22:40:10 UTC
Permalink
Post by Olivier B.
thank you, this bypass my problem, however calculate the length in
request is an additional load for my SQL server, there is no way to
compare strictly spaces?
You could convert to binary, but that would be worse, as it would kill
any index on the column. I think using datalength is the best.

By the standrads for the SQL standard, trailing spaces does not count
when comparing strings.
--
Erland Sommarskog, SQL Server MVP, ***@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
Olivier B.
2012-12-14 21:35:47 UTC
Permalink
On Wed, 12 Dec 2012 23:40:10 +0100, Erland Sommarskog
Post by Erland Sommarskog
Post by Olivier B.
thank you, this bypass my problem, however calculate the length in
request is an additional load for my SQL server, there is no way to
compare strictly spaces?
You could convert to binary, but that would be worse, as it would kill
any index on the column. I think using datalength is the best.
ok
Post by Erland Sommarskog
By the standrads for the SQL standard, trailing spaces does not count
when comparing strings.
thanks !
--
pas de turlututu. apres l'@robase
Loading...