Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find non-default collation on columns for all tables in SQL Server

Is there a script I can use to find all columns in all tables in a SQL catalog that does not have the collation <database default> ?

I have taken over a legacy system and have different collations in the some tables and I would like to find all instances in one hit rather than going through manually.

However I don't want to change the collation programmatically as I would like to review each table one by one.

enter image description here

like image 552
Rippo Avatar asked Dec 13 '11 11:12

Rippo


People also ask

How do I find the column collation in SQL Server?

To view the collation setting of a databaseIn Object Explorer, connect to an instance of the Database Engine and on the toolbar, click New Query. In the query window, enter the following statement that uses the sys. databases system catalog view. SELECT name, collation_name FROM sys.

How do you find the collation from a table?

Finding collation of a column in a table. In SSMS, go to the table, then columns, and finally right-click the individual columns to view the “Properties”. If the column is of a character data type, you will see details of the collation.

Is SQL_Latin1_General_CP1_CI_AS the same as Latin1_General_CI_AS?

The SQL_Latin1_General_CP1_CI_AS collation is a SQL collation and the rules around sorting data for unicode and non-unicode data are different. The Latin1_General_CI_AS collation is a Windows collation and the rules around sorting unicode and non-unicode data are the same.


1 Answers

Try this script here:

DECLARE @DatabaseCollation VARCHAR(100)  SELECT      @DatabaseCollation = collation_name  FROM      sys.databases WHERE      database_id = DB_ID()  SELECT      @DatabaseCollation 'Default database collation'  SELECT      t.Name 'Table Name',     c.name 'Col Name',     ty.name 'Type Name',     c.max_length,     c.collation_name,     c.is_nullable FROM      sys.columns c  INNER JOIN      sys.tables t ON c.object_id = t.object_id INNER JOIN      sys.types ty ON c.system_type_id = ty.system_type_id     WHERE      t.is_ms_shipped = 0     AND      c.collation_name <> @DatabaseCollation 

It checks for the database default collation and then finds any columns that don't comply with that.

like image 123
marc_s Avatar answered Sep 20 '22 04:09

marc_s