What is the N prefix in MSSQL all about?
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
, orNTEXT
data type, rather thanCHAR
,VARCHAR
, orTEXT
. -
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. TheVARCHAR
column is restricted to an 8-bit. -
By using
NVARCHAR
rather thanVARCHAR
, we can avoid doing encoding conversions every time we read from or write to the database. Some people think thatVARCHAR
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.
- The
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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12707874/viewspace-2885837/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- What you should know about JavaJava
- 論文翻譯:2022_Phase-Aware Deep Speech Enhancement: It’s All About The Frame Length
- 023 Given an integer n. get the number of 0, 2, 4 from all the values from [0, n]
- E - Prefix Equality
- about me
- About HTMLHTML
- Natasha, Sasha and the Prefix Sums
- IP-Prefix List
- Prefix Flip (Easy Version)
- Trivia about pythonPython
- About My Blog
- All I know about A/B Test (1) : 均值型指標與比值(率)型指標的計算區別指標
- Leetcode 14 Longest Common PrefixLeetCode
- What is wrong?
- what is life?
- What is WebpackWeb
- What is maven?Maven
- What is Babel?Babel
- Narrative writing about a person
- An example about git hookGitHook
- About the Oracle GoldenGate TrailOracleGoAI
- 3.4.1 About Quiescing a DatabaseUIDatabase
- 2.3.3.1 About Application MaintenanceAPPAINaN
- 2.3.1 About Application ContainersAPPAI
- 1.Prefix字首和【模板】
- 【MSSQL】MSSQL 從Express版本升級到Enterprise版本SQLExpress
- mssql程式塊SQL
- What is a service mesh?
- What is rate limiting?MIT
- WHAT IS PPM Encoder ?
- What is an SQL relation?SQL
- Some notes about patch workflows
- Some ideas About ‘invisible bug‘Idea
- Notes about Vue Style GuideVueGUIIDE
- Something about seniority in the family or clan
- Codeforces 1485F Copy or Prefix Sum
- MSSQL Rebuild(重建)索引SQLRebuild索引
- onethink如何支援MSSQL?SQL