Is the LIKE operator case-sensitive with MSSQL Server?

后端 未结 8 647
清歌不尽
清歌不尽 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:53

    It is not the operator that is case sensitive, it is the column itself.

    When a SQL Server installation is performed a default collation is chosen to the instance. Unless explicitly mentioned otherwise (check the collate clause bellow) when a new database is created it inherits the collation from the instance and when a new column is created it inherits the collation from the database it belongs.

    A collation like sql_latin1_general_cp1_ci_as dictates how the content of the column should be treated. CI stands for case insensitive and AS stands for accent sensitive.

    A complete list of collations is available at https://msdn.microsoft.com/en-us/library/ms144250(v=sql.105).aspx

    (a) To check a instance collation

    select serverproperty('collation')
    

    (b) To check a database collation

    select databasepropertyex('databasename', 'collation') sqlcollation
    

    (c) To create a database using a different collation

    create database exampledatabase
    collate sql_latin1_general_cp1_cs_as 
    

    (d) To create a column using a different collation

    create table exampletable (
        examplecolumn varchar(10) collate sql_latin1_general_cp1_ci_as null
    )
    

    (e) To modify a column collation

    alter table exampletable
    alter column examplecolumn varchar(10) collate sql_latin1_general_cp1_ci_as null
    

    It is possible to change a instance and database collations but it does not affect previously created objects.

    It is also possible to change a column collation on the fly for string comparison, but this is highly unrecommended in a production environment because it is extremely costly.

    select
      column1 collate sql_latin1_general_cp1_ci_as as column1
    from table1
    
    0 讨论(0)
  • 2020-11-27 14:53

    Try running,

    SELECT SERVERPROPERTY('COLLATION')
    

    Then find out if your collation is case sensitive or not.

    0 讨论(0)
提交回复
热议问题