Is the LIKE operator case-sensitive with MSSQL Server?

后端 未结 8 776
清歌不尽
清歌不尽 2020-11-27 14:15

In the documentation about the LIKE operator, nothing is told about the case-sensitivity of it. Is it? How to enable/disable it?

I am querying varchar(n)

8条回答
  •  感动是毒
    2020-11-27 14:48

    The like operator takes two strings. These strings have to have compatible collations, which is explained here.

    In my opinion, things then get complicated. The following query returns an error saying that the collations are incompatible:

    select *
    from INFORMATION_SCHEMA.TABLES
    where 'abc' COLLATE SQL_Latin1_General_CP1_CI_AS like 'ABC' COLLATE SQL_Latin1_General_CP1_CS_AS
    

    On a random machine here, the default collation is SQL_Latin1_General_CP1_CI_AS. The following query is successful, but returns no rows:

    select *
    from INFORMATION_SCHEMA.TABLES
    where 'abc' like 'ABC' COLLATE SQL_Latin1_General_CP1_CS_AS
    

    The values "abc" and "ABC" do not match in a case-sensitve world.

    In other words, there is a difference between having no collation and using the default collation. When one side has no collation, then it is "assigned" an explicit collation from the other side.

    (The results are the same when the explicit collation is on the left.)

提交回复
热议问题