question

chana avatar image
chana asked

NVarchar datatype is unable to handle Unicode character

NVarchar datatype is unable to handle Unicode character like "Minyā"
unicodedata-types
1 comment
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

insert into table1 (col1, col2) values(convert(nvarchar(100) ,"‘prhān" ), 1) I want the data to be inserted as prhān but it is inserted as prhan.
0 Likes 0 ·
Cyborg avatar image
Cyborg answered
Its working!


DECLARE @nv NVARCHAR(20)
SET @nv = N'Minyā'
SELECT @nv


10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

WilliamD avatar image
WilliamD answered
What Cyborg forgot to mention is the necessity of adding the capital N before the quoted text. This tells SQL Server that it has to respect unicode coding and your example will work: SELECT CONVERT(nvarchar(100), 'prhān') -- N missing, result incorrect SELECT CONVERT(nvarchar(100), N'prhān') -- N included, result correct
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Cyborg avatar image
Cyborg answered
You have to Use 'N' with NVARCHAR, The N' prefix indicates national character, or nchar/nvarchar data. It tells SQL Server to convert a string into nchar.

CREATE TABLE #nVarcharDemo
(C1 INT,
C2 NVARCHAR(10)
)
 
 
 INSERT INTO #nVarcharDemo
 -- Will Insert as "prhan" since you did't used "N"
 SELECT 1, CONVERT(NVARCHAR,'prhān')
 UNION ALL
 -- Insert with 'N'
 SELECT 2, CONVERT(NVARCHAR,N'prhān')
 
 SELECT * FROM #nVarcharDemo

2 comments
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

+1. But why use CONVERT? Why not do *INSERT INTO #nVarcharDemo SELECT 1, N'prhān'*
0 Likes 0 ·
I was following @chana Query thats why, Nothing Important.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.