COLLATE (Transact-SQL) - SQL Server (2024)

  • Article

Applies to: COLLATE (Transact-SQL) - SQL Server (1) SQL Server COLLATE (Transact-SQL) - SQL Server (2) Azure SQL Database COLLATE (Transact-SQL) - SQL Server (3) Azure SQL Managed Instance COLLATE (Transact-SQL) - SQL Server (4) Azure Synapse Analytics COLLATE (Transact-SQL) - SQL Server (5) Analytics Platform System (PDW) COLLATE (Transact-SQL) - SQL Server (6) Warehouse in Microsoft Fabric

Defines a collation of a database or table column, or a collation cast operation when applied to character string expression. Collation name can be either a Windows collation name or a SQL collation name. If not specified during database creation, the database is assigned the default collation of the instance of SQL Server. If not specified during table column creation, the column is assigned the default collation of the database.

COLLATE (Transact-SQL) - SQL Server (7) Transact-SQL syntax conventions

Syntax

COLLATE { <collation_name> | database_default }<collation_name> :: = { Windows_collation_name } | { SQL_collation_name }

Arguments

collation_nameIs the name of the collation to be applied to the expression, column definition, or database definition. collation_name can be only a specified Windows_collation_name or a SQL_collation_name. collation_name must be a literal value. collation_name cannot be represented by a variable or expression.

Windows_collation_name is the collation name for a Windows Collation Name.

SQL_collation_name is the collation name for a SQL Server Collation Name.

database_defaultCauses the COLLATE clause to inherit the collation of the current database.

Remarks

The COLLATE clause can be specified at several levels. These include the following:

  1. Creating or altering a database.

    You can use the COLLATE clause of the CREATE DATABASE or ALTER DATABASE statement to specify the default collation of the database. You can also specify a collation when you create a database using SQL Server Management Studio. If you do not specify a collation, the database is assigned the default collation of the instance of SQL Server.

    Note

    Windows Unicode-only collations can only be used with the COLLATE clause to apply collations to the nchar, nvarchar, and ntext data types on column-level and expression-level data; these cannot be used with the COLLATE clause to define or change the collation of a database or server instance.

  2. Creating or altering a table column.

    You can specify collations for each character string column using the COLLATE clause of the CREATE TABLE or ALTER TABLE statement. You can also specify a collation when you create a table using SQL Server Management Studio. If you do not specify a collation, the column is assigned the default collation of the database.

    You can also use the database_default option in the COLLATE clause to specify that a column in a temporary table use the collation default of the current user database for the connection instead of tempdb.

  3. Casting the collation of an expression.

    You can use the COLLATE clause to apply a character expression to a certain collation. Character literals and variables are assigned the default collation of the current database. Column references are assigned the definition collation of the column.

The collation of an identifier depends on the level at which it is defined. Identifiers of instance-level objects, such as logins and database names, are assigned the default collation of the instance. Identifiers of objects within a database, such as tables, views, and column names, are assigned the default collation of the database. For example, two tables with names different only in case may be created in a database with case-sensitive collation, but may not be created in a database with case-insensitive collation. For more information, see Database Identifiers.

Variables, GOTO labels, temporary stored procedures, and temporary tables can be created when the connection context is associated with one database, and then referenced when the context has been switched to another database. The identifiers for variables, GOTO labels, temporary stored procedures, and temporary tables are in the default collation of the server instance.

The COLLATE clause can be applied only for the char, varchar, text, nchar, nvarchar, and ntext data types.

COLLATE uses collate_name to refer to the name of either the SQL Server collation or the Windows collation to be applied to the expression, column definition, or database definition. collation_name can be only a specified Windows_collation_name or a SQL_collation_name and the parameter must contain a literal value. collation_name cannot be represented by a variable or expression.

Collations are generally identified by a collation name, except in Setup. In Setup, you instead specify the root collation designator (the collation locale) for Windows collations, and then specify sort options that are sensitive or insensitive to case or accents.

You can execute the system function fn_helpcollations to retrieve a list of all the valid collation names for Windows collations and SQL Server collations:

SELECT name, descriptionFROM fn_helpcollations();

SQL Server can support only code pages that are supported by the underlying operating system. When you perform an action that depends on collations, the SQL Server collation used by the referenced object must use a code page supported by the operating system running on the computer. These actions can include the following:

  • Specifying a default collation for a database when you create or alter the database.
  • Specifying a collation for a column when you create or alter a table.
  • When restoring or attaching a database, the default collation of the database and the collation of any char, varchar, and text columns or parameters in the database must be supported by the operating system.

Note

Code page translations are supported for char and varchar data types, but not for text data type. Data loss during code page translations is not reported.

If the collation specified or the collation used by the referenced object uses a code page not supported by Windows, SQL Server displays an error.

Examples

A. Specifying collation during a SELECT

The following example creates a simple table and inserts 4 rows. Then the example applies two collations when selecting data from the table, demonstrating how Chiapas is sorted differently.

CREATE TABLE Locations(Place varchar(15) NOT NULL);GOINSERT Locations(Place) VALUES ('Chiapas'),('Colima') , ('Cinco Rios'), ('California');GO--Apply an typical collationSELECT Place FROM LocationsORDER BY PlaceCOLLATE Latin1_General_CS_AS_KS_WS ASC;GO-- Apply a Spanish collationSELECT Place FROM LocationsORDER BY PlaceCOLLATE Traditional_Spanish_ci_ai ASC;GO

Here are the results from the first query.

Place-------------CaliforniaChiapasCinco RiosColima

Here are the results from the second query.

Place-------------CaliforniaCinco RiosColimaChiapas

B. Additional examples

For additional examples that use COLLATE, see CREATE DATABASE example G. Creating a database and specifying a collation name and options, and ALTER TABLE example V. Changing column collation.

See Also

  • ALTER TABLE
  • Collation and Unicode Support
  • Collation Precedence
  • Constants
  • CREATE DATABASE
  • CREATE TABLE
  • DECLARE @local_variable
  • Table data type
COLLATE (Transact-SQL) - SQL Server (2024)
Top Articles
Why Do We Have Armpit Hair? And Other Body Hair Answers
Paytm once had India's largest IPO. Now a central bank crackdown could kill its business: 'A risk that the political system couldn’t take'
Evil Dead Movies In Order & Timeline
Woodward Avenue (M-1) - Automotive Heritage Trail - National Scenic Byway Foundation
Umbc Baseball Camp
Where are the Best Boxing Gyms in the UK? - JD Sports
Don Wallence Auto Sales Vehicles
Nc Maxpreps
The Idol - watch tv show streaming online
Pwc Transparency Report
Premier Reward Token Rs3
Shreveport Active 911
Fear And Hunger 2 Irrational Obelisk
Destiny 2 Salvage Activity (How to Complete, Rewards & Mission)
Saatva Memory Foam Hybrid mattress review 2024
50 Shades Of Grey Movie 123Movies
Eine Band wie ein Baum
Quadcitiesdaily
Scout Shop Massapequa
Never Give Up Quotes to Keep You Going
European city that's best to visit from the UK by train has amazing beer
6 Most Trusted Pheromone perfumes of 2024 for Winning Over Women
2021 MTV Video Music Awards: See the Complete List of Nominees - E! Online
Vera Bradley Factory Outlet Sunbury Products
Waters Funeral Home Vandalia Obituaries
Tactical Masters Price Guide
Dailymotion
Dentist That Accept Horizon Nj Health
Kaiser Infozone
Kids and Adult Dinosaur Costume
Have you seen this child? Caroline Victoria Teague
Emily Katherine Correro
Martin Village Stm 16 & Imax
How to Draw a Bubble Letter M in 5 Easy Steps
Craigslist Ludington Michigan
Ark Unlock All Skins Command
Trizzle Aarp
Firestone Batteries Prices
2007 Jaguar XK Low Miles for sale - Palm Desert, CA - craigslist
Pain Out Maxx Kratom
Levi Ackerman Tattoo Ideas
Juiced Banned Ad
Toomics - Die unendliche Welt der Comics online
Yourcuteelena
Www Pig11 Net
Christie Ileto Wedding
Craiglist.nj
Google Flights Missoula
18443168434
Ubg98.Github.io Unblocked
Costco Gas Price Fort Lauderdale
Equinox Great Neck Class Schedule
Latest Posts
Article information

Author: Greg Kuvalis

Last Updated:

Views: 6391

Rating: 4.4 / 5 (75 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Greg Kuvalis

Birthday: 1996-12-20

Address: 53157 Trantow Inlet, Townemouth, FL 92564-0267

Phone: +68218650356656

Job: IT Representative

Hobby: Knitting, Amateur radio, Skiing, Running, Mountain biking, Slacklining, Electronics

Introduction: My name is Greg Kuvalis, I am a witty, spotless, beautiful, charming, delightful, thankful, beautiful person who loves writing and wants to share my knowledge and understanding with you.