61

How can I check to see if a database in SQL Server is case-sensitive? I have previously been running the query:

SELECT CASE WHEN 'A' = 'a' THEN 'NOT CASE SENSITIVE' ELSE 'CASE SENSITIVE' END

But I am looking for other ways as this has actually given me issues in the past.

Edit - A little more info: An existing product has many pre-written stored procedures. In a stored procedure @test != @TEST depending on the sensitivity of the server itself. So what I'm looking for is the best way to check the server for its sensitivity.

shA.t
  • 16,580
  • 5
  • 54
  • 111
Kyle
  • 17,317
  • 32
  • 140
  • 246

7 Answers7

85

Collation can be set at various levels:

  1. Server
  2. Database
  3. Column

So you could have a Case Sensitive Column in a Case Insensitive database. I have not yet come across a situation where a business case could be made for case sensitivity of a single column of data, but I suppose there could be.

Check Server Collation

SELECT SERVERPROPERTY('COLLATION')

Check Database Collation

SELECT DATABASEPROPERTYEX('AdventureWorks', 'Collation') SQLCollation;

Check Column Collation

select table_name, column_name, collation_name
from INFORMATION_SCHEMA.COLUMNS
where table_name = @table_name
JustinKSU
  • 4,875
  • 2
  • 29
  • 51
Raj More
  • 47,048
  • 33
  • 131
  • 198
  • 28
    For the sake of completeness, you can set the collation also in SQL using `SELECT * FROM foo where x = 'y' COLLATE sql_latin1_general_cp1_cs_as` – Tim Büthe Nov 15 '11 at 12:50
  • 10
    Just adding one more piece to the puzzle: if the collation name contains _CI_ it's case-insensitive, if it contains _CS_ it's case-sensitive. [Read more in MSDN](https://msdn.microsoft.com/en-us/library/ms180175.aspx) – Zohar Peled Apr 21 '15 at 13:25
  • There are many things there are case sensitive, like URLs, passwords so there you go, examples of case sensitive columns. – gdoron Feb 18 '16 at 16:57
  • 5
    @Gordon I hope you're not storing passwords in plaintext in your database! – Jeff Meden Oct 19 '16 at 20:48
  • @gdoron: I've yet to see a case sensitive URL. https://stackoverflow.com/questions/7996919/should-url-be-case-sensitive . Sure, the get part can be, but not the domain. – Mitch Wheat Jun 05 '19 at 08:07
  • @JeffMeden 3 years later I just saw your comment. No, I'm not storing passwords as plain text, anyway, hashed passwords are case sensitive. – gdoron Jun 05 '19 at 13:21
  • @MitchWheat we just had a big bug in my company because Google changed one of its websites to be case sensitive. They are real and they are spectacular. – gdoron Jun 05 '19 at 13:22
  • @gdoron: you are saying that google changed the domain part of a URL to be case sensitive? What's the URL? – Mitch Wheat Jun 06 '19 at 03:15
  • @MitchWheat, no, where did you see my writing about the domain? I wrote URLs, domains are case insensitive. paths and querystrings on the other hand aren't. – gdoron Jun 06 '19 at 06:17
  • @gdoron which is what my comment states above! -> "Sure, the get part can be, but not the domain." – Mitch Wheat Jun 07 '19 at 00:00
37

If you installed SQL Server with the default collation options, you might find that the following queries return the same results:

CREATE TABLE mytable 
( 
    mycolumn VARCHAR(10) 
) 
GO 

SET NOCOUNT ON 

INSERT mytable VALUES('Case') 
GO 

SELECT mycolumn FROM mytable WHERE mycolumn='Case' 
SELECT mycolumn FROM mytable WHERE mycolumn='caSE' 
SELECT mycolumn FROM mytable WHERE mycolumn='case' 

You can alter your query by forcing collation at the column level:

SELECT myColumn FROM myTable  
    WHERE myColumn COLLATE Latin1_General_CS_AS = 'caSE' 

SELECT myColumn FROM myTable  
    WHERE myColumn COLLATE Latin1_General_CS_AS = 'case' 

SELECT myColumn FROM myTable  
    WHERE myColumn COLLATE Latin1_General_CS_AS = 'Case' 

-- if myColumn has an index, you will likely benefit by adding 
-- AND myColumn = 'case' 

SELECT DATABASEPROPERTYEX('<database name>', 'Collation') 

As changing this setting can impact applications and SQL queries, I would isolate this test first. From SQL Server 2000, you can easily run an ALTER TABLE statement to change the sort order of a specific column, forcing it to be case sensitive. First, execute the following query to determine what you need to change it back to:

EXEC sp_help 'mytable' 

The second recordset should contain the following information, in a default scenario:

Column_Name Collation


mycolumn SQL_Latin1_General_CP1_CI_AS

Whatever the 'Collation' column returns, you now know what you need to change it back to after you make the following change, which will force case sensitivity:

ALTER TABLE mytable 
    ALTER COLUMN mycolumn VARCHAR(10) 
    COLLATE Latin1_General_CS_AS 
GO 



SELECT mycolumn FROM mytable WHERE mycolumn='Case' 
SELECT mycolumn FROM mytable WHERE mycolumn='caSE' 
SELECT mycolumn FROM mytable WHERE mycolumn='case' 

If this screws things up, you can change it back, simply by issuing a new ALTER TABLE statement (be sure to replace my COLLATE identifier with the one you found previously):

ALTER TABLE mytable 
    ALTER COLUMN mycolumn VARCHAR(10) 
    COLLATE SQL_Latin1_General_CP1_CI_AS 

If you are stuck with SQL Server 7.0, you can try this workaround, which might be a little more of a performance hit (you should only get a result for the FIRST match):

SELECT mycolumn FROM mytable WHERE 
    mycolumn = 'case' AND 
    CAST(mycolumn AS VARBINARY(10)) = CAST('Case' AS VARBINARY(10)) 

SELECT mycolumn FROM mytable WHERE 
    mycolumn = 'case' AND 
    CAST(mycolumn AS VARBINARY(10)) = CAST('caSE' AS VARBINARY(10)) 

SELECT mycolumn FROM mytable WHERE 
    mycolumn = 'case' AND 
    CAST(mycolumn AS VARBINARY(10)) = CAST('case' AS VARBINARY(10)) 

-- if myColumn has an index, you will likely benefit by adding 
-- AND myColumn = 'case' 
Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162
13

SQL server determines case sensitivity by COLLATION.

COLLATION can be set at various levels.

  1. Server-level
  2. Database-level
  3. Column-level
  4. Expression-level

Here is the MSDN reference.

One can check the COLLATION at each level as mentioned in Raj More's answer.

Check Server Collation

SELECT SERVERPROPERTY('COLLATION')

Check Database Collation

SELECT DATABASEPROPERTYEX('AdventureWorks', 'Collation') SQLCollation;

Check Column Collation

select table_name, column_name, collation_name
from INFORMATION_SCHEMA.COLUMNS
where table_name = @table_name

Check Expression Collation

For expression level COLLATION you need to look at the expression. :)

It would be generally at the end of the expression as in below example.

SELECT name FROM customer ORDER BY name COLLATE Latin1_General_CS_AI;

Collation Description

For getting description of each COLLATION value try this.

SELECT * FROM fn_helpcollations()

And you should see something like this.

enter image description here

You can always put a WHERE clause to filter and see description only for your COLLATION.

You can find a list of collations here.

Community
  • 1
  • 1
Devraj Gadhavi
  • 3,541
  • 3
  • 38
  • 67
4

You're interested in the collation. You could build something based on this snippet:

SELECT DATABASEPROPERTYEX('master', 'Collation');

Update
Based on your edit — If @test and @TEST can ever refer to two different variables, it's not SQL Server. If you see problems where the same variable is not equal to itself, check if that variable is NULL, because NULL = NULL returns `false.

shA.t
  • 16,580
  • 5
  • 54
  • 111
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 1
    `NULL = NULL` test would be dependant on ANSI_NULLS isn't it? `where NULL IS NULL` would return true (Handling NULL Values in MSSQL Queries)[http://www.peter-urda.com/2010/11/handling-null-values-in-mssql-queries] – Paul C Jan 04 '13 at 16:58
2

The best way to work with already created tables is that, Go to Sql Server Query Editor

Type: sp_help <tablename>

This will show table's structure , see the details for the desired field under COLLATE column.

then type in the query like :

SELECT myColumn FROM myTable  
WHERE myColumn COLLATE SQL_Latin1_General_CP1_CI_AS = 'Case'

It could be different character schema <SQL_Latin1_General_CP1_CI_AS>, so better to find out the exact schema that has been used against that column.

DareDevil
  • 5,249
  • 6
  • 50
  • 88
1

How can I check to see if a database in SQL Server is case-sensitive?

You can use below query that returns your informed database is case sensitive or not or is in binary sort(with null result):

;WITH collations AS (
    SELECT 
        name,
        CASE 
            WHEN description like '%case-insensitive%' THEN 0 
            WHEN description like '%case-sensitive%' THEN 1 
        END isCaseSensitive
    FROM 
        sys.fn_helpcollations()
)
SELECT *
FROM collations
WHERE name = CONVERT(varchar, DATABASEPROPERTYEX('yourDatabaseName','collation'));

For more read this MSDN information ;).

shA.t
  • 16,580
  • 5
  • 54
  • 111
-3

SQL Server is not case sensitive. SELECT * FROM SomeTable is the same as SeLeCT * frOM soMetaBLe.

shA.t
  • 16,580
  • 5
  • 54
  • 111
  • That's not what he's talking about. He means how _data_ is compared, not how code is processed. – Joel Coehoorn Sep 11 '09 at 14:22
  • I think the question is about comparing data in a case-insensitive way - not whether T-SQL statements are case-sensitive. – Vinay Sajip Sep 11 '09 at 14:22
  • 2
    The way it is worded, it sounds like he was asking if it was case sensitive. I think the down vote is a little unfair there. – Mark Callison Sep 11 '09 at 14:24
  • 3
    The query he provided makes it pretty clear what he's asking about. – Cybergibbons Sep 11 '09 at 14:29
  • I guess we will have to agree to disagree on that one – Mark Callison Sep 11 '09 at 14:32
  • 2
    Looking at the OPs recent edit where he's talking about parameters and variables of mixed case, I think Mark's got a valid point here. It's not purely data in columns. Going to +1. – Chris J Sep 11 '09 at 15:06
  • 1
    Did +1 but maybe expand as 'SQL Server is not case sensitive' is not entirely true as a broad statement when referring to data comparison – Paul C Jan 04 '13 at 17:01
  • 2
    "SQL Server is not case sensitive" is not true. Whether SQL Server is case sensitive for data, *or for schema* (e.g. table names, field names) is dependent on collation settings. Remember that table names/field names are just metadata which is also affected by collation settings. – Rob Levine Jan 25 '13 at 10:10
  • At MarkCallison: I doubt anyone would be asking whether Reserved Words are case-sensitive, but user-defined Identifiers would be a fair question. @RobLevine: What about Variables (which I doubt are stored in System Tables at least not permanent ones) - is there there some SQL Server setting that would make *them* case-sensitive (ala C#)? – Tom Jun 22 '17 at 20:08