How can I make SQL Server return FALSE for comparing varchars with and without trailing spaces?

后端 未结 6 643
刺人心
刺人心 2020-12-10 12:55

If I deliberately store trailing spaces in a VARCHAR column, how can I force SQL Server to see the data as mismatch?

SELECT \'foo\' WHERE \'bar\         


        
6条回答
  •  生来不讨喜
    2020-12-10 13:55

    After some search the simplest solution i found was in Anthony Bloesch WebLog.

    Just add some text (a char is enough) to the end of the data (append)

    SELECT 'foo' WHERE 'bar' + 'BOGUS_TXT' = 'bar    ' + 'BOGUS_TXT'
    

    Also works for 'WHERE IN'

    SELECT 
    FROM 
    WHERE  + 'BOGUS_TXT' in ( SELECT  + 'BOGUS_TXT' FROM  )
    

提交回复
热议问题