July 15, 2008 at 3:06 am
Hi all
I have a query which compare two string type "nvarchar"
Something like this:
print 'True'
else print 'False'
It is simple, but when i input something like
@a='1'
@b-2='1 ' ( with some blank space )
Then the query is still return "True" althought what i mean is "False"
I googled this and see that sql server auto insert some character to sorter string(in many case, it is a blank space,correct me if i am wrong :)) before compare those string.
So, have you guys have any idea to solve this.
Thanks very much.
Sol 😀
July 15, 2008 at 5:04 am
SQL ignores trailing spaces when comparing strings. It's probably because of the char data type, which is space padded to max size
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 15, 2008 at 5:32 am
It is part of the ANSI standard to ignore these trailing spaces. It is done automatically in joins as well. If you find yourself in a situation in which you care about trailing spaces you should try to re-think the design.
July 15, 2008 at 9:45 pm
To @hari.Shama:
Thanks for your help.
I checked and it worked but it seem confuses to me when use this one.
To @gilamonster and @michael-2 Earl:
Yeah, You're right.
Thanks for all of your help.
To resolve this, i use "datalength" function which return length of string include space, so i can compare like this:
If Datalength('1') = Datalength('1 ')
print 'True'
elseprint 'False'
Thanks and regards.
Sol
July 15, 2008 at 10:05 pm
I myself had this issue. And aftere searching here and there, I found that the equality (=) operator elininates the trailing spaces and then compare. If you want to compare the two variables with the trailing spaces, use LIKE operator.
i.e....
Declare @a varchar(10)
Declare @b-2 varchar(10)
Select @a = '1'
Select @b-2 = '1 '
print 'True'
else
print 'False'
--Will Print True
print 'True'
else
print 'False'
--Will Print False
Atif Sheikh
July 15, 2008 at 10:57 pm
I too think 'Like' is the best solution as suggested above.
July 16, 2008 at 5:53 am
You can also do this 🙂
if ('c'+@a+'c') = ('c'+@b+'c' )
print 'True'
else print 'False'
July 16, 2008 at 8:57 am
July 16, 2008 at 11:37 pm
Personaly, I don't think use "like" is a good idea.
Because user can input "%" or "*" in their input string and so, your condition will fail to compare these values.
Sol
July 17, 2008 at 12:24 am
sol (7/16/2008)
Personaly, I don't think use "like" is a good idea.Because user can input "%" or "*" in their input string and so, your condition will fail to compare these values.
Sol
to clarify - when you have just spaces like in ur case the question asked, 'like' is a good solution.
July 17, 2008 at 8:06 pm
Hari.Sharma (7/15/2008)
Hi Sol,Try this
DECLARE @a varchar(10),@b varchar(10)
SET @a='1'
SET @b-2='1 '
if QUOTENAME(@a) = QUOTENAME(@b)
print 'True'
else
print 'False'
Seems like the easiest way to go to me! 😉
Now, the real question is why anyone would be storing trailing blanks for anything...
--Jeff Moden
Change is inevitable... Change for the better is not.
July 31, 2008 at 4:10 am
to clarify - when you have just spaces like in ur case the question asked, 'like' is a good solution.
Yeah, I understood what you mean.
Regards
Sol
Viewing 14 posts - 1 through 13 (of 13 total)
You must be logged in to reply to this topic. Login to reply