SQL LIKE with no wildcards the same as '='?

前端 未结 3 1782
醉梦人生
醉梦人生 2020-12-11 00:05

I know this is a pretty basic question, and I think I know the answer...but I\'d like to confirm.

Are these queries truly equivalent?

SELECT         


        
3条回答
  •  猫巷女王i
    2020-12-11 00:55

    Original Answer by Matt Whitfield from here

    There is a difference between = and LIKE. When you perform string comparisons by using LIKE, all characters in the pattern string are significant. This includes leading or trailing spaces.

    So if you have a column that is char or nchar and not nvarchar or varchar, there will be different results due to trailing spaces.

    Small example to reproduce this behaviour:

    CREATE TABLE #temp (nam [varchar](MAX))
    INSERT INTO [#temp] ([nam])
    VALUES ('hello')
    INSERT INTO [#temp] ([nam])
    VALUES ('hello  ')
    
    SELECT * FROM #temp WHERE [nam] = 'hello  '
    
    SELECT * FROM #temp WHERE [nam] LIKE 'hello  '
    

提交回复
热议问题