SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


CASTing


CASTing

Author
Message
john.arnott
john.arnott
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3648 Visits: 3059
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


Aleksandr Furman
Aleksandr Furman
Mr or Mrs. 500
Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)

Group: General Forum Members
Points: 577 Visits: 125
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
john.arnott
john.arnott
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3648 Visits: 3059
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)


Anipaul
Anipaul
SSCrazy Eights
SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)SSCrazy Eights (9.9K reputation)

Group: General Forum Members
Points: 9861 Visits: 1407
Good Questions ............ Tongue



Simon Facer
Simon Facer
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2441 Visits: 724
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.



Christian Buettner-167247
Christian Buettner-167247
SSCertifiable
SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)SSCertifiable (5.3K reputation)

Group: General Forum Members
Points: 5347 Visits: 3889
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
Aleksandr Furman
Aleksandr Furman
Mr or Mrs. 500
Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)Mr or Mrs. 500 (577 reputation)

Group: General Forum Members
Points: 577 Visits: 125
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
Simon Facer
Simon Facer
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2441 Visits: 724
[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... Ermm



gurmeet.singh
gurmeet.singh
Valued Member
Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)Valued Member (62 reputation)

Group: General Forum Members
Points: 62 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 .....
Peter E. Kierstead
Peter E. Kierstead
SSC Eights!
SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)SSC Eights! (888 reputation)

Group: General Forum Members
Points: 888 Visits: 453
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search