How to store UniCode strings in SQL Server 2019

Chris Sijtsma 40 Reputation points
2023-11-02T07:03:46.5666667+00:00

Dear colleagues,

When I try the following script from Managament Studio, the result is not what I would like.

SELECT 'أفغانستان'
UNION ALL SELECT 'Афганистан'
UNION ALL SELECT 'Αφγανιστάν'
UNION ALL SELECT 'Աֆղանստան'
UNION ALL SELECT 'Afghanistan'
UNION ALL SELECT 'アフガニスタン' 
UNION ALL SELECT '아프가니스탄' 
UNION ALL SELECT 'Afeganistão'
UNION ALL SELECT 'Афганистан'
UNION ALL SELECT 'อัฟกานิสถาน'
UNION ALL SELECT 'Афганістан'
UNION ALL SELECT '阿富汗';

The result:

UTF8

Obviously, I have to pick the right collation. But how do I find out which collation to use for which string?

Developer technologies Transact-SQL
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2023-11-02T22:49:16.8133333+00:00

    Actually, since you are on SQL 2019, you don't need the N. You can take any collation that ends in UTF8.

    CREATE DATABASE UTF8Demo COLLATE Pashto_100_CI_AS_SC_UTF8
    go
    USE UTF8Demo
    go
    SELECT 'أفغانستان'
    UNION ALL SELECT 'Афганистан'
    UNION ALL SELECT 'Αφγανιστάν'
    UNION ALL SELECT 'Աֆղանստան'
    UNION ALL SELECT 'Afghanistan'
    UNION ALL SELECT 'アフガニスタン' 
    UNION ALL SELECT '아프가니스탄' 
    UNION ALL SELECT 'Afeganistão'
    UNION ALL SELECT 'Афганистан'
    UNION ALL SELECT 'อัฟกานิสถาน'
    UNION ALL SELECT 'Афганістан'
    UNION ALL SELECT '阿富汗';
    
    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Chris Sijtsma 40 Reputation points
    2023-11-02T07:09:13.7233333+00:00

    I'm sorry, beginners mistake. It is so obvious. The answer (do note the N' denoting unicode strings):

    SELECT N'أفغانستان'
    UNION ALL SELECT N'Афганистан'
    UNION ALL SELECT N'Αφγανιστάν'
    UNION ALL SELECT N'Աֆղանստան'
    UNION ALL SELECT N'Afghanistan'
    UNION ALL SELECT N'アフガニスタン' 
    UNION ALL SELECT N'아프가니스탄' 
    UNION ALL SELECT N'Afeganistão'
    UNION ALL SELECT N'Афганистан'
    UNION ALL SELECT N'อัฟกานิสถาน'
    UNION ALL SELECT N'Афганістан'
    UNION ALL SELECT N'阿富汗'
    
    0 comments No comments

  2. Chris Sijtsma 40 Reputation points
    2023-11-03T14:56:44.2066667+00:00

    Thank you, Erland. As ever, I highly value your input.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.