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 353995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:52:57+00:00 2026-05-12T11:52:57+00:00

I ran into something a little odd this morning and thought I’d submit it

  • 0

I ran into something a little odd this morning and thought I’d submit it for commentary.

Can someone explain why the following SQL query prints ‘equal’ when run against SQL 2008. The db compatibility level is set to 100.

if '' = ' '
    print 'equal'
else
    print 'not equal'

And this returns 0:

select (LEN(' '))

It appears to be auto trimming the space. I have no idea if this was the case in previous versions of SQL Server, and I no longer have any around to even test it.

I ran into this because a production query was returning incorrect results. I cannot find this behavior documented anywhere.

Does anyone have any information on this?

  • 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-05-12T11:52:57+00:00Added an answer on May 12, 2026 at 11:52 am

    varchars and equality are thorny in TSQL. The LEN function says:

    Returns the number of characters, rather than the number of bytes, of the given string expression, excluding trailing blanks.

    You need to use DATALENGTH to get a true byte count of the data in question. If you have unicode data, note that the value you get in this situation will not be the same as the length of the text.

    print(DATALENGTH(' ')) --1
    print(LEN(' '))        --0
    

    When it comes to equality of expressions, the two strings are compared for equality like this:

    • Get Shorter string
    • Pad with blanks until length equals that of longer string
    • Compare the two

    It’s the middle step that is causing unexpected results – after that step, you are effectively comparing whitespace against whitespace – hence they are seen to be equal.

    LIKE behaves better than = in the “blanks” situation because it doesn’t perform blank-padding on the pattern you were trying to match:

    if '' = ' '
    print 'eq'
    else
    print 'ne'
    

    Will give eq while:

    if '' LIKE ' '
    print 'eq'
    else
    print 'ne'
    

    Will give ne

    Careful with LIKE though: it is not symmetrical: it treats trailing whitespace as significant in the pattern (RHS) but not the match expression (LHS). The following is taken from here:

    declare @Space nvarchar(10)
    declare @Space2 nvarchar(10)
    
    set @Space = ''
    set @Space2 = ' '
    
    if @Space like @Space2
    print '@Space Like @Space2'
    else
    print '@Space Not Like @Space2'
    
    if @Space2 like @Space
    print '@Space2 Like @Space'
    else
    print '@Space2 Not Like @Space'
    
    @Space Not Like @Space2
    @Space2 Like @Space
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into something odd today that maybe someone knows something about. I have
I ran into something odd, and I'm not precisely sure why it is behaving
This originally was a problem I ran into at work, but is now something
I ran into something that seems odd. SQL Server appears to be rounding some
Ok this must be something dumb. I ran into this when moving some code
I ran into a problem... perhaps someone bumped into something similar: I have an
Ran into something interesting, want to know if I'm doing something wrong or if
I just ran into something in Visual Studio 2010 RC that wasn't previously happening
Today I ran into something a bit weird. I used mini-profiler to see the
I was doing some performance metrics and I ran into something that seems quite

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.