LCJ
LCJ

Reputation: 22652

SQL Server WHERE Condition does not consider white spaces

I have first name and last name with trailing white spaces. I have two SQL queries – the first query is returning result even if I search with no white space. (The first query is returning unwanted result).

  1. Is this behavior consistent in all versions of SQL Server?
  2. Is it a known behavior? Is it documented anywhere in msdn for SQL Server 2008 R2?

CODE

DECLARE @NameTable TABLE (first_name varchar(40),last_name varchar(40))
INSERT INTO @NameTable VALUES ('STEVEN    ','STANLEY   ');


--QUERY 1
SELECT first_name AS [FirstName], last_name AS [LastName]
FROM  @NameTable A
WHERE (first_name = 'STEVEN')
AND (last_name = 'STANLEY')


--QUERY 2       
SELECT first_name AS [FirstName], last_name AS [LastName]
FROM  @NameTable A
WHERE (( ISNULL(first_name,'')+' ' +ISNULL(last_name,'') ) = 'STEVEN STANLEY')

--QUERY 3   (With LTRIM and RTRIM)    
SELECT first_name AS [FirstName], last_name AS [LastName]
FROM  @NameTable A
WHERE (( ISNULL(LTRIM(RTRIM(first_name)),'')+' ' +ISNULL(LTRIM(RTRIM(last_name)),'') ) = 'STEVEN STANLEY')

Reference:

  1. DataLength
  2. In SQL Server 2005, what is the difference between len() and datalength()?

Upvotes: 3

Views: 4865

Answers (2)

bummi
bummi

Reputation: 27367

Refer http://support.microsoft.com/kb/316626

SQL Server follows the ANSI/ISO SQL-92 specification (Section 8.2, , General rules #3) on how to compare strings with spaces. The ANSI standard requires padding for the character strings used in comparisons so that their lengths match before comparing them. The padding directly affects the semantics of WHERE and HAVING clause predicates and other Transact-SQL string comparisons. For example, Transact-SQL considers the strings 'abc' and 'abc ' to be equivalent for most comparison operations.

The only exception to this rule is the LIKE predicate. When the right side of a LIKE predicate expression features a value with a trailing space, SQL Server does not pad the two values to the same length before the comparison occurs. Because the purpose of the LIKE predicate, by definition, is to facilitate pattern searches rather than simple string equality tests, this does not violate the section of the ANSI SQL-92 specification mentioned earlier.

If you want to avoid this you can add an additions condition, would look like the following

Refer: DataLength

SELECT first_name AS [FirstName], last_name AS [LastName],first_name+last_name,LEN(first_name+last_name)
FROM  @NameTable A
WHERE (first_name = 'STEVEN') and DATALENGTH(first_name)=DATALENGTH(RTRIM(first_name))
AND (last_name = 'STANLEY') and DATALENGTH(last_name)=DATALENGTH(RTRIM(last_name))

Upvotes: 5

Vinayak Pahalwan
Vinayak Pahalwan

Reputation: 3005

I can't say however i've something related with comparison of strings with trailing white space --- http://support.microsoft.com/kb/316626

(EDIT)

Can you check this code?

SELECT first_name AS [FirstName], last_name AS [LastName]
FROM  @NameTable A
WHERE (left(first_name,6) = 'STEVEN')+' '+(right(last_name,6) = 'STANLEY')

(for names with 6 characters)

Upvotes: 2

Related Questions