
I used to learn about SQL, especially MySQL. Then now my job requires me to understand Microsoft SQL Server (mssql). So I make these notes to guide me and others who want to transition to mssql. I usually summarized what I found and understood so far from browsing the internet.
My takeaways:
It's declaring the string as
NCHAR,
NVARCHAR, or
NTEXT data type, rather than
CHAR,
VARCHAR, or
TEXT.
This N prefix denotes that subsequent string is in Unicode (the N actually stands for National language character set).
To quote from Microsoft:
Without the N prefix, the string is converted to the default code page of the database and may not recognize certain characters.
The
NVARCHAR column can store any Unicode data. The
VARCHAR column is restricted to an 8-bit.
By using
NVARCHAR rather than
VARCHAR, we can
avoid doing encoding conversions every time we read from or write to the database. Some people think that
VARCHAR should be used because it takes up less space. Codepage incompatibilities are pain and Unicode is the cure for codepage problems.
The
VARCHAR data type represents the
non-Unicode variable-length string data type. We can store letters, numbers, and special characters in it.
N represents string size in bytes ( not a value representing the number of characters in a string). It stores a maximum of 8000 Non-Unicode characters. It takes 1 byte per character. If we do not explicitly specify the value for N, it takes 1-byte storage.
-- DECLARE @var AS VARCHAR(N) = 'text';DECLARE @text AS VARCHAR(100) = 'this VARCHAR yields 33 characters';SELECT
@text AS Output,
DATALENGTH(@text) AS Length;
The query above defines
VARCHAR data type with 100 bytes of data.
It returns the
string length as 33 because of 1 byte per character, including a space character.

NVARCHAR data type is for the
Unicode variable-length character data type. We can store
both non-Unicode and Unicode characters (Japanese Kanji, Korean Hangul, etc.).
N represents
string size in bytes (
not a value representing the number of characters in a string).
It stores a
maximum of 4000 Unicode and Non-Unicode characters.
It takes
2 byte per character. If we do not
explicitly specify the value for N, it takes
2-byte storage.
-- DECLARE @var AS NVARCHAR(N) = N'text';DECLARE @text AS NVARCHAR(100) = N'this NVARCHAR yields 34 characters';SELECT
@text AS Output,
DATALENGTH(@text) AS Length;
The query above defines
NVARCHAR data type with 100 bytes of data.
It returns the
string length of 68 because
NVARCHAR takes 2 bytes per character.

Storing example:
CREATE TABLE UserComments(
ID int IDENTITY (1,1),
[Language] VARCHAR(50),
[Comment] VARCHAR(200),
[NewComment] NVARCHAR(200));INSERT INTO UserComments ([Language],[Comment],[NewComment]) VALUES ('English','Hello World', N'Hello World');INSERT INTO UserComments ([Language],[Comment],[NewComment]) VALUES ('Japanese','こんにちは世界', N'こんにちは世界');INSERT INTO UserComments ([Language],[Comment],[NewComment]) VALUES ('Hindi','नमस्ते दुनिया', N'नमस्ते दुनिया');SELECT * FROM UserComments;

原文: https://dev.to/ranggakd/what-is-the-n-prefix-in-mssql-all-about-44kf
编辑推荐:
下一篇:
相关推荐
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
热文推荐
- 2条流程解决数据同步到全球部署的N个数据库
2条流程解决数据同步到全球部署的N个数据库
26-03-02
- What is the N prefix in MSSQL all about?
- 华为电脑管家安装不了解决方法图解教程
华为电脑管家安装不了解决方法图解教程
26-03-02
- 虎牙、斗鱼财报表现分道扬镳
虎牙、斗鱼财报表现分道扬镳
26-03-02
- SSMS 显示行号
SSMS 显示行号
26-03-02
- Ubuntu mpstat命令详解(CPU性能统计与系统监控入门指南)
Ubuntu mpstat命令详解(CPU性能统计与系统监控入门指南)
26-03-02
- What is dbo in SQL Server?
What is dbo in SQL Server?
26-03-02
- 电脑显卡带ti和不带ti的区别在哪_显卡有ti没ti差多少性能
电脑显卡带ti和不带ti的区别在哪_显卡有ti没ti差多少性能
26-03-02
- 戴尔台式机intel 8代cpu装win7系统及BIOS设置教程完美支持
戴尔台式机intel 8代cpu装win7系统及BIOS设置教程完美支持
26-03-02
- 苹果电脑如何压缩视频_视频太大怎么把视频压缩变小
苹果电脑如何压缩视频_视频太大怎么把视频压缩变小
26-03-02
