Comparison of empty string with string containing space characters

  • Hi All,

            I'm trying to achieve a functionality by comparing an empty string with a non-empty string (string containing space characters). SQL server treats this kind of comparison to be equal.

    Sample code:

    IF('' = '   ')

    Print 'Equal'

    Else

    Print 'Not Equal'

    Executing the above SQL code in SQL query analyzer will print 'Equal' instead of 'Not Equal'. Can anyone tell whether this is due to some server setting or do we have any workarond to solve this problem.

    Thanks,

    John.

  • Try replacing your = with LIKE.  It's better to use the LIKE indicator when comparing string information.  This should give you your expected results.

  • SQL Server 2000 follows the ANSI SQL standards when comparing strings of different lengths, the shorter string is padded with blanks and then compared. So SQL Server 2000 is correct in saying that an empty string equals a string of 1 blank.

  • For this purpuse, how about

    IF Len(@a) Len(@b)

    PRINT 'Not Equal'

    ELSE IF @a LIKE @b-2

    PRINT 'Equal'

    ELSE

    PRINT 'Not Equal'

  • Got it. Thanks for the reply.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply