The pit (RPM) encountered in SQL Server comparing strings with spaces at the end

Source: Internet
Author: User

Http://www.cnblogs.com/happycat1988/p/4779805.html

Recently found when comparing strings in SQL Server if the end of the string is a space then SQL Server ignores those spaces and compares them directly to the usual string judgment logic in the program.

Declare @a nvarchar, set @a=n ' happycat1988 '  declare @b nvarchar (a); set @b=n ' happycat1988 '  if (@a = @b)      Select ' True ' as direct equals comparison else      select ' False ' as direct equal equals comparison        if (@a like @b)      select ' True ' as like compare else      select ' Fa LSE ' as like comparison

The results of the above query execution are as follows

Direct equals------True (1 rows affected) Like compare------False (1 rows affected)

From the above can be seen when the direct equal judgement of the SQL will ignore the end of the space but like but can be the correct comparison although with a kind is also a method but if with a% and so may judge the error can not only use as a judge, but anyway, is to attach a condition Is there anything more simple than like? And then I thought about using the Len function with the string length comparison and then stepping on the other pit.

Declare @a nvarchar, set @a=n ' happycat1988 '  declare @b nvarchar (a); set @b=n ' happycat1988 '  select Len (@a) ' Len of a ', Len (@b) ' Len of B ', datalength (@a) ' datalength of a ', datalength (@b) ' Datalength of B '

The results of the above query execution are as follows

Len of a    len of B    datalength of a datalength of B----------------------------------------------------          12< C5/>24              26 (1 rows affected)

It is also possible to see that the Len function ignores the trailing spaces only by using the DATALENGTH function as the basis for additional precise judgments.

The description of the query for MSDN (Transact-SQL) Discovery Len function is "Returns the number of characters of the specified string expression, excluding Tra Iling Blanks. "That is to exclude the space comparison, so to swap with datalength

So we've sorted out some exact ways to judge strings.

Declare @a nvarchar, set @a=n ' happycat1988 '  declare @b nvarchar (a); set @b=n ' happycat1988 '  if (@a = @b and Dat Alength (@a) =datalength (@b))      select ' True ' as mate Datalength compare  else       select ' False ' as mate Datalength compare       if (@a = @b and @a like @b and @b like @a)      Select ' true ' as mates like compare else       select ' False ' as mates like compare     if (@a + ' a ' = @b + ' a ')      select ' True ' as the end of the supplemental character comparison 
   else       Select ' False ' as end-of-complement character comparison  

The results of the above query execution are as follows

With datalength comparison--------------false (1 rows affected) Mate like compare--------false (1 rows affected) End complement character comparison--------false (1 rows affected)

Hoping to help the same pit-stomping friends.

The pit (RPM) encountered in SQL Server comparing strings with spaces at the end

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.