How to define "charset" of a table in SQL Server?

2

In MySQL to set the charset of a table we can use this way:

MySQL:

create table user_details (...) default character set = utf8;

How can I do the same in SQL Server?

    
asked by anonymous 07.04.2016 / 14:46

2 answers

4

First, UTF-8 is an encoding and not a charset . The charset to be chosen depends somewhat on the encoding chosen. The same goes for the rules of conversion and search of the text.

Not possible. SQL Server does not support this encoding. Put the text in a nvarchar column. If this is the case you can specify a collate among the available ones, but none of them will use the encoding UTF-8.

If you need the data in this encoding you need to have the application handle UTF-8 and deliver it as you wish, but the data will not be written to this format. A client specific to SQL Server could even handle this transparently. I do not know if anyone does.

If absolutely necessary you could use varbinary and do coding on your own, but I doubt this is any better than conversion.

Documentation .

    
07.04.2016 / 15:01
1

You will need to change the column definition to be NVARCHAR . to have support unicode.

I found a very interesting example on the internet to show how you can do this

Code:

USE Test
GO

/* Create Test table with non-unicode column */
IF OBJECT_ID('dbo.Test', 'U') IS NOT NULL
BEGIN
DROP TABLE dbo.Test
END

CREATE TABLE dbo.Test (Col1 VARCHAR(20))
GO


SELECT character_set_name,collation_name FROM information_schema.columns WHERE table_name = 'test'

/* Change to support unicode and ensure collation */
ALTER TABLE dbo.test ALTER COLUMN col1 NVARCHAR(20) COLLATE latin1_General_CI_AS
GO

SELECT character_set_name,collation_name  FROM information_schema.columns 
WHERE table_name = 'test'

Summarizing , you will have to convert Column Varchar to Nvarchar     

07.04.2016 / 15:02