Unicode String Literals in SQL Server Transact-SQL

Q

What are Unicode string literals supported in SQL Server Transact-SQL?

✍: FYIcenter.com

A

Unicode string literals in Transact-SQL are sequences of characters enclosed in single quotes and prefixed with (N) as N'...'.

String literals are used to provide values to string variables or table columns like NCHAR(40), NVARCHAR(40), etc.

Unicode string literals are also used in string operations in expressions and string arguments in functions.

Here are some simple rules on how to write string literals:

  • Unicode string literals must be enclosed in single quotes and prefixed with (N) like N'...'.
  • A single quote inside the string must be entered twice like N'Murphy''s law'.
  • Each character in a Unicode string is represented in 2 bytes using the UTF-16 encoding.

Here are some example on how to write string literals:

PRINT N'FYIcenter.com';
PRINT N'Murphy''s law';

DECLARE @var NCHAR(2);
SET @var = N'友爱';
PRINT @var;

 

Unicode Character String Literals in SQL Server Transact-SQL

CHAR(n) - Truncating/Padding Strings in SQL Server Transact-SQL

Data Literals in SQL Server Transact-SQL

⇑⇑ SQL Server Transact-SQL Tutorials

2017-05-05, 1920🔥, 0💬