Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing SQL Server collation to case insensitive from case sensitive?

I've recently installed SQL Server 2008 and I selected collation as case sensitive. I want to make it case insensitive for the entire instance (not for a database in that instance). If I change the collation does it affect any existing databases? if so in what way?

like image 979
JPReddy Avatar asked Jul 21 '10 05:07

JPReddy


People also ask

How do I make SQL Server database case sensitive insensitive?

SQL Server is, by default case insensitive; however, it is possible to create a case sensitive SQL Server database and even to make specific table columns case sensitive. The way to determine a database or database object is by checking its “COLLATION” property and look for “CI” or “CS” in the result.

How do you change a database from case sensitive to case-insensitive?

If you have your database built with case insensitve collation but would like to do case sensitive search just use the COLLATE keyword. For example the first select will find all records where test code is 'ABC' regardless of the case - so abc, AbC aBc and such will be found.

What is collation case-insensitive?

A case-insensitive collation ignores the differences between uppercase and lowercase letters for string comparison and sorting, whereas a case-sensitive collation does not. For example, in case-insensitive collation, “A” and “a” are equal.


2 Answers

You basically need to run the installation again to rebuild the master database with the new collation. You cannot change the entire server's collation any other way.

See:

  • MSDN: Setting and changing the server collation
  • How to change database or server collation (in the middle of the page)

Update: if you want to change the collation of a database, you can get the current collation using this snippet of T-SQL:

SELECT name, collation_name  FROM sys.databases WHERE name = 'test2'   -- put your database name here 

This will yield a value something like:

Latin1_General_CI_AS 

The _CI means "case insensitive" - if you want case-sensitive, use _CS in its place:

Latin1_General_CS_AS 

So your T-SQL command would be:

ALTER DATABASE test2 -- put your database name here    COLLATE Latin1_General_CS_AS   -- replace with whatever collation you need 

You can get a list of all available collations on the server using:

SELECT * FROM ::fn_helpcollations() 

You can see the server's current collation using:

SELECT SERVERPROPERTY ('Collation') 
like image 110
marc_s Avatar answered Sep 28 '22 03:09

marc_s


You can do that but the changes will affect for new data that is inserted on the database. On the long run follow as suggested above.

Also there are certain tricks you can override the collation, such as parameters for stored procedures or functions, alias data types, and variables are assigned the default collation of the database. To change the collation of an alias type, you must drop the alias and re-create it.

You can override the default collation of a literal string by using the COLLATE clause. If you do not specify a collation, the literal is assigned the database default collation. You can use DATABASEPROPERTYEX to find the current collation of the database.

You can override the server, database, or column collation by specifying a collation in the ORDER BY clause of a SELECT statement.

like image 41
Satya SKJ Avatar answered Sep 28 '22 03:09

Satya SKJ