|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
1. The "N" prefix indicates "National" characters, that is, double-byte characters to support large-alphabet languages such as Japanese. 2. The concatenation of N'hello' to the repeated hyphens is where the first conversion to national characters happens. 3. Although a variable defined as varchar(max) or nvarchar(max) may hold many more characters, without the "max", the largest size is 8000 bytes (4000 double-byte characters). 4. The assignment of the concatenated string to @c converts the nvarchar data back to varchar, but it's already truncated at 4000 characters
Try running the code with different values to see either the truncation in effect or parsing errors on the maximum size of a data type.
Finishes with same output as original:
declare @c varchar(7000) set @c = N'hello' + replicate('-',9000) print len(@c) print @c
Max size error:
declare @c varchar(9000) set @c = N'hello' + replicate('-',9000) print len(@c) print @c
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Thursday, February 07, 2013 12:08 PM
Points: 441,
Visits: 92
|
|
About size of nvarchar(max)
I tried this:
declare @c nvarchar(max) set @c = N'hello' + replicate('-',8000) print len(@c)
the result was still 4000. Can someone explain why it wasn't more since nvarchar(max) can accept more then 4000
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
Aleksandr, The truncation happens in the concatenation of the hyphens to N'hello', step 2 in my previous post. It's truncated because the string 'hello' is cast as National characters, so the concatenation is kept as double-bytes with a limit of 4000. This is before the string is assigned to the local variable, so even though @c can hold more, the 4000 characters is all that is sent to it.
Again, try it out.....
declare @c nvarchar(max) set @c = Convert(nvarchar(max), N'hello') + replicate('-',9500) print len(@c) set @c = Convert(nvarchar(max), N'hello') + convert(nvarchar(max),replicate('-',9500)) print len(@c) set @c = Convert(nvarchar(max), N'hello') + replicate(convert(nvarchar(max),'-'),9500) print len(@c) set @c = N'hello' + replicate(convert(nvarchar(max),'-'),9500) print len(@c)
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Yesterday @ 6:03 AM
Points: 4,787,
Visits: 1,335
|
|
Good Questions ............ :P
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 7:30 AM
Points: 1,566,
Visits: 600
|
|
Aleksandr Furman (5/27/2008)
About size of nvarchar(max) I tried this: declare @c nvarchar(max) set @c = N'hello' + replicate('-',8000) print len(@c)
the result was still 4000. Can someone explain why it wasn't more since nvarchar(max) can accept more then 4000
The REPLICATE('-', 8000) has an implicit cast to NVARCHAR, try this to explicitly CAST it to NVARCHAR(MAX):
declare @c nvarchar(max) set @c = N'hello' + CAST(replicate('-',8000) AS NVARCHAR(MAX)) print len(@c) you get 8005 as the length.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Today @ 1:06 AM
Points: 2,525,
Visits: 3,618
|
|
Simon Facer (5/27/2008)
The REPLICATE('-', 8000) has an implicit cast to NVARCHAR Sorry Simon, this is not correct (or at least misleading).
The type conversion happens during the concatenation of the two strings.
-- Example 1: -- Replicating a non-unicode string results in a non-unicode string -- (Datalength = Lenght) SELECT DATALENGTH(REPLICATE('-', 8000)), Len(REPLICATE('-', 8000))
-- Example 2: Concatenating a unicode string and -- a non-unicode string results in a unicode string -- (Datalength = 2* Lenght) SELECT DATALENGTH(N'Unicode' + 'Nonunicode'), LEN(N'Unicode' + 'Nonunicode')
Best Regards,
Chris Büttner
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Thursday, February 07, 2013 12:08 PM
Points: 441,
Visits: 92
|
|
Simon Facer (5/27/2008)
The REPLICATE('-', 8000) has an implicit cast to NVARCHAR, try this to explicitly CAST it to NVARCHAR(MAX): declare @c nvarchar(max) set @c = N'hello' + CAST(replicate('-',8000) AS NVARCHAR(MAX)) print len(@c) you get 8005 as the length. What I found very interesting is that to REPLICATE more then 8000 characters you have to cast inside value:
convert(nvarchar(max),replicate('-',9500)) - This gets you 8000 characters vs. replicate(convert(nvarchar(max),'-'),9500) - This gets you 9500 characters
Thank you John
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 7:30 AM
Points: 1,566,
Visits: 600
|
|
[b]Christian Buettner (5/27/2008)...Sorry Simon, this is not correct (or at least misleading)...
Sorry, it could be construed as misleading. My assumption was that everyone would understand that the discussion was about NVARCHAR fields, seeing as that was the focus of the QOTD. My bad...
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Tuesday, February 07, 2012 10:15 AM
Points: 60,
Visits: 34
|
|
Some More Clarifications related to Varchar And NVarchar
VARCHAR supports variable length strings, up to 8,000 characters. Best used when data length is variable, e.g. last names or product SKU codes.
NVARCHAR Similar to VARCHAR, with the support of Unicode characters. You should only use this datatype if you need Unicode support - due to storage overhead (2 bytes per character means the maximum length of your string is 4,000 characters).
Good Question .....
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, March 25, 2013 8:37 AM
Points: 163,
Visits: 275
|
|
The answer to this question is technically correct... While Len(@c) is in fact 4000 the type and maximum len of @c is VarChar, 8000. Try concatenating another 4000 VarChar characters to @c and then look at the metrics!
declare @c varchar(8000)
set @c = N'hello' + replicate('-',8000) print len(@c) print @c
set @c=@c+replicate('*',4000) print len(@c) print @c
The leading type of the R-value (right-hand side of the assignment operator) is nVarChar, therefore then entire R-value was cast into this type, hence the max len 4000.
PeteK I have CDO. It's like OCD but all the letters are in alphabetical order... as they should be.
|
|
|
|