Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««123»»

CASTing Expand / Collapse
Author
Message
Posted Tuesday, May 27, 2008 10:24 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, August 28, 2014 8:53 PM
Points: 1,521, Visits: 3,039
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

Post #507068
Posted Tuesday, May 27, 2008 11:32 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 8:22 AM
Points: 453, Visits: 102
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
Post #507124
Posted Tuesday, May 27, 2008 11:57 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Thursday, August 28, 2014 8:53 PM
Points: 1,521, Visits: 3,039
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)

Post #507139
Posted Tuesday, May 27, 2008 12:03 PM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 3:19 AM
Points: 5,368, Visits: 1,391
Good Questions ............ :P


Post #507146
Posted Tuesday, May 27, 2008 12:51 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, September 15, 2014 1:24 PM
Points: 1,570, Visits: 675
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.



Post #507192
Posted Tuesday, May 27, 2008 3:09 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 1:05 AM
Points: 2,842, Visits: 3,875
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
Post #507291
Posted Tuesday, May 27, 2008 3:27 PM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 8:22 AM
Points: 453, Visits: 102
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
Post #507296
Posted Tuesday, May 27, 2008 5:23 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, September 15, 2014 1:24 PM
Points: 1,570, Visits: 675
[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...



Post #507333
Posted Tuesday, May 27, 2008 10:48 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Tuesday, February 7, 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 .....
Post #507414
Posted Wednesday, May 28, 2008 8:08 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, June 20, 2014 2:22 PM
Points: 190, Visits: 436
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.
Post #507688
« Prev Topic | Next Topic »

Add to briefcase ««123»»

Permissions Expand / Collapse