Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9018407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:31:10+00:00 2026-06-16T04:31:10+00:00

I have first name and last name with trailing white spaces. I have two

  • 0

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()?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:31:12+00:00Added an answer on June 16, 2026 at 4:31 am

    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))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables: DRIVER(Driver_Id,First name,Last name,...); PARTICIPANT IN CAR ACCIDENT(Participant_Id,Driver_Id-foreign key,responsibility-yes or no,...).
I have three text fields: first name, last name, and age, also a photo.
I have a form that includes the first name and last name of a
I have a customer table where I'm currently storing: Last Name First Name Middle
So I have these tables: STUDENTS: Student ID - First name - Last name
I have an html: First name: <input type='text' name='first_name' value='' /><br/> Last name: <input
Let's say I have a form like this Username Nickname First Name Last Name
I have first name and last name in separate fields in my database. I'd
Say I have: @Given(first name is $firstName) @Given(first name is $firstName and last name
I have a User Information table which contains First name last name ... When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.