Files
sql-server-samples/samples/features/unicode/notebooks/Functional.ipynb
T
2019-11-21 09:38:37 -08:00

16 KiB

UTF-8 Functional Demo

Make sure databases don't exist

In [1]:
USE master;
GO

DROP DATABASE IF EXISTS MyNonUtf8Database;

DROP DATABASE IF EXISTS MyUtf8Database;

DROP DATABASE IF EXISTS MyFormerlyUnicodeOnlyDatabase;

DROP DATABASE IF EXISTS MaskingDatabase;
Commands completed successfully.
Commands completed successfully.
Total execution time: 00:00:00.129

Create a database that is NOT collated with UTF-8.

This demonstrates that you can insert Unicode data into VARCHAR columns collated with UTF-8. This demo used the N' syntax, as string literals are always collated in the collation of the currently active database.

In [2]:
CREATE DATABASE MyNonUtf8Database COLLATE SQL_Latin1_General_CP1_CI_AI;
GO

USE MyNonUtf8Database;
GO

CREATE TABLE MyUtf8Table (datakind VARCHAR(100), data VARCHAR(8000) COLLATE Latin1_General_100_CI_AS_SC_UTF8);
GO

INSERT INTO MyUtf8Table
VALUES ('ASCII - 1 byte per character', N'Thequickbrownfoxjumpsoverthelazydog'), 
	('Cyrillic - 2 bytes per character', N'Быстраякоричневаялисапрыгаетчерезленивуюсобаку'), 
	('Far East - 3 bytes per character', N'敏捷的棕色狐狸跳过了懒狗'), 
	('Emojis - 4 bytes per character', N'👶👦👧👨👩👴👵👨👩👨👩👨👩👨👩'), 
	('Emojis with Variation Selector - 6 bytes per glyph', N'⚕️⚖️↔︎↕︎↖︎↗︎↘︎↙︎↩︎↪︎↔️↕️↖️↗️↘️↙️↩️↪️'), 
	('Ashi with Supplementary Variation Selector - 7 bytes per glyph', N'芦󠄀芦󠄁芦󠄂芦󠄃芦󠄄芦󠄅芦󠄆芦󠄇芦󠄈芦󠄉芦󠄃芦󠄂芦󠄁芦󠄀芦󠄁芦󠄂芦󠄃芦󠄄芦󠄈芦󠄉');
GO

SELECT datakind, data
FROM MyUtf8Table;
GO
Out [2]:
Commands completed successfully.
Commands completed successfully.
Commands completed successfully.
(6 rows affected)
(6 rows affected)
Total execution time: 00:00:00.597
datakinddata
ASCII - 1 byte per characterThequickbrownfoxjumpsoverthelazydog
Cyrillic - 2 bytes per characterБыстраякоричневаялисапрыгаетчерезленивуюсобаку
Far East - 3 bytes per character敏捷的棕色狐狸跳过了懒狗
Emojis - 4 bytes per character👶👦👧👨👩👴👵👨👩👨👩👨👩👨👩
Emojis with Variation Selector - 6 bytes per glyph⚕️⚖️↔︎↕︎↖︎↗︎↘︎↙︎↩︎↪︎↔️↕️↖️↗️↘️↙️↩️↪️
Ashi with Supplementary Variation Selector - 7 bytes per glyph芦󠄀芦󠄁芦󠄂芦󠄃芦󠄄芦󠄅芦󠄆芦󠄇芦󠄈芦󠄉芦󠄃芦󠄂芦󠄁芦󠄀芦󠄁芦󠄂芦󠄃芦󠄄芦󠄈芦󠄉

Create a database collated with UTF-8.

This is to demonstrate that now string literals can be used without N' as string literals are collated with the database collation, and can hold any characters.

In [1]:
CREATE DATABASE MyUtf8Database COLLATE Lithuanian_100_CS_AI_WS_SC_UTF8;
GO

USE MyUtf8Database;
GO

CREATE TABLE MyTableWithInheritedCollation (datakind VARCHAR(100), data VARCHAR(8000));
GO

INSERT INTO MyTableWithInheritedCollation
VALUES ('ASCII - 1 byte per character', 'Thequickbrownfoxjumpsoverthelazydog'), 
	('Cyrillic - 2 bytes per character', 'Быстраякоричневаялисапрыгаетчерезленивуюсобаку'), 
	('Far East - 3 bytes per character', '敏捷的棕色狐狸跳过了懒狗'), 
	('Emojis - 4 bytes per character', '👶👦👧👨👩👴👵👨👩👨👩👨👩👨👩'), 
	('Emojis with Variation Selector - 6 bytes per glyph', '⚕️⚖️↔︎↕︎↖︎↗︎↘︎↙︎↩︎↪︎↔️↕️↖️↗️↘️↙️↩️↪️'), 
	('Ashi with Supplementary Variation Selector - 7 bytes per glyph', '芦󠄀芦󠄁芦󠄂芦󠄃芦󠄄芦󠄅芦󠄆芦󠄇芦󠄈芦󠄉芦󠄃芦󠄂芦󠄁芦󠄀芦󠄁芦󠄂芦󠄃芦󠄄芦󠄈芦󠄉');
GO

SELECT datakind, data
FROM MyTableWithInheritedCollation;
GO
Out [1]:
Commands completed successfully.
Commands completed successfully.
Commands completed successfully.
(6 rows affected)
(6 rows affected)
Total execution time: 00:00:00.424
datakinddata
ASCII - 1 byte per characterThequickbrownfoxjumpsoverthelazydog
Cyrillic - 2 bytes per characterБыстраякоричневаялисапрыгаетчерезленивуюсобаку
Far East - 3 bytes per character敏捷的棕色狐狸跳过了懒狗
Emojis - 4 bytes per character👶👦👧👨👩👴👵👨👩👨👩👨👩👨👩
Emojis with Variation Selector - 6 bytes per glyph⚕️⚖️↔︎↕︎↖︎↗︎↘︎↙︎↩︎↪︎↔️↕️↖️↗️↘️↙️↩️↪️
Ashi with Supplementary Variation Selector - 7 bytes per glyph芦󠄀芦󠄁芦󠄂芦󠄃芦󠄄芦󠄅芦󠄆芦󠄇芦󠄈芦󠄉芦󠄃芦󠄂芦󠄁芦󠄀芦󠄁芦󠄂芦󠄃芦󠄄芦󠄈芦󠄉