Storing a Non-English String in Table – Unicode Strings in SQL SERVER


In SQL Server, Unicode strings can be used to store non-English strings in tables. Unicode is a character encoding standard that supports a wide range of characters from different languages and scripts.

To store a non-English string in a table, follow these steps:

  • Define the column as an nvarchar data type. The nvarchar data type is used to store Unicode strings.

Example:

CREATE TABLE MyTable (
   ID int,
   Name nvarchar(50)
);
  • Insert the non-English string into the table using the N prefix before the string. The N prefix tells SQL Server that the string is a Unicode string.

Example:

INSERT INTO MyTable (ID, Name) VALUES (1, N'Привет, мир!');
  • Retrieve the non-English string from the table using the N prefix before the string.

Example:

SELECT Name FROM MyTable WHERE ID = 1;

Output: Привет, мир!

Another method to store non-English strings in SQL Server is to use the COLLATE clause. The COLLATE clause specifies the character set and collation for the column.

Example:

CREATE TABLE MyTable (
   ID int,
   Name varchar(50) COLLATE Cyrillic_General_CI_AS
);

In the above example, the Cyrillic_General_CI_AS collation is used to store Cyrillic characters.

Insert the non-English string into the table without the N prefix.

Example:

INSERT INTO MyTable (ID, Name) VALUES (1, 'Привет, мир!');

Retrieve the non-English string from the table using the COLLATE clause.

Example:

SELECT Name COLLATE Cyrillic_General_CI_AS FROM MyTable WHERE ID = 1;

Output: Привет, мир!

In conclusion, to store non-English strings in SQL Server, use the nvarchar data type with the N prefix or use the COLLATE clause with the appropriate collation for the character set.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.