Consider the following SQL
DECLARE @JSON VARCHAR(max);
DECLARE @t AS TABLE(
field char(32),
len1 int,
nfield nchar(32),
nlen1 int,
vfield varchar(32),
vlen1 int
);
SET @JSON = '[
{ "Field" : "abcd" }
]'
INSERT INTO @t
SELECT
Field as field,
Len(Field) as len1,
Field as nfield,
Len(Field) as nlen1,
Field as vfield,
Len(Field) as vlen1
FROM OPENJSON (@JSON)
WITH ( Field nchar(32) );
INSERT INTO @t ( field , len1 ,nfield, nlen1 ,vfield, vlen1 )
VALUES ( 'efgh', len('efgh'),'efgh', len('efgh'),'efgh', len('efgh'))
SELECT
field,
len1,
len(field) as len2,
nlen1,
len(nfield) as nlen2,
vlen1,
len(vfield) as vlen2
FROM @t;
which outputs
field len1 len2 nlen1 nlen2 vlen1 vlen2
------ ----- ----- ------ ------ ------ ------
abcd 4 4 4 32 4 4
efgh 4 4 4 4 4 4
The length of the value originating from JSON in the nchar(32)
field is 32 while all others are the length of the string. This was a surprise to me so I'm looking for an underlying explanation.
What is happening that this one case of the 12 has a different result?