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


CASTing


CASTing

Author
Message
Carlo Romagnano
Carlo Romagnano
SSCertifiable
SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)SSCertifiable (7.4K reputation)

Group: General Forum Members
Points: 7375 Visits: 3395
Comments posted to this topic are about the item CASTing

I run on tuttopodismo
John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33093 Visits: 16637
Sorry, I don't understand. What CAST to nvarchar(800)? Is there a typo in the question?

John
Jerry Hung
Jerry Hung
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3310 Visits: 1208
me a little bit confused too, what's the difference between this Question, and the one 2 days ago?

TODAY

CASTing

Second question of day: what is the len of @c?

declare @c varchar(800)

set @c = N'hello' + replicate('-',800)
print len(@c)
print @c

Sorry - you were wrong

Correct answer: 800
Explanation:
The CAST to nvarchar(800) has a maximum 4000 character len. The CAST then to varchar(800) fits in that space, so the len is 800


2 days ago

CASTing

First question of day: what is the len of @c?

declare @c varchar(8000)

set @c = N'hello' + replicate('-',8000)
print len(@c)
print @c

You got it right!

Correct answer: 4000
Explanation:
The CAST to NVARCHAR(4000) means that the maximum len is 4000, then the cast to varchar(8000) allows more characters, but the string is already truncated.


SQLServerNewbie

MCITP: Database Administrator SQL Server 2005
steveb.
steveb.
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10068 Visits: 7195
The one two days ago was casting to a length greater than the nvarchar data type max of 4000, so the length was truncated to 4000. The one today was casting to a length less than 4000 so it stayed the same.
Jerry Hung
Jerry Hung
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3310 Visits: 1208
Does that mean this 2 lines below, converts @c to nvarchar(8000)?
meaning ANY casting to nvarchar always give it maximum length of 8000?

declare @c varchar(800)
set @c = N'hello'



I think I got lost by reading this sentence ... maybe I shouldn't read it anymore

The CAST to nvarchar(800) has a maximum 4000 character len.


SQLServerNewbie

MCITP: Database Administrator SQL Server 2005
steveb.
steveb.
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10068 Visits: 7195
nvarchar has a max length of 4000 not 8000
the 'N' in this statment converts it;


set @c = N'hello'


TheRedneckDBA
TheRedneckDBA
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4113 Visits: 2613
Good question of the day...I totally fell for it!

The Redneck DBA
John Mitchell-245523
John Mitchell-245523
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33093 Visits: 16637
Yeah, so did I. I see what happened now.

John
Biggles-581128
Biggles-581128
SSC Veteran
SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)

Group: General Forum Members
Points: 257 Visits: 208
Great question.

Didn't fall into the trap, but only because I remembered the question 2 days ago and thought there must be something else going on.

Its got me thinking a lot more about types and implicit conversions. Thanks
Christian Buettner-167247
Christian Buettner-167247
SSCertifiable
SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)SSCertifiable (5.2K reputation)

Group: General Forum Members
Points: 5177 Visits: 3889
I think these two QODs really show how evil implicit type conversions are.
Id rather spend this one second on explicit type conversion than on reviewing such code for possible data loss caused by implicit conversion.
I had to look at this QOD for at least 5 seconds before I made my choice. Sum this up for all your code and then tell me: wouldn't it be great if the compiler told you when you had missed a type conversion?

Sure, you have a test team, but why not catch an error in the first place?
Isn't a database a realm where datatypes should be handled in the most strict way?

Just some thoughts in the late evening :-)

Best Regards,

Chris Büttner
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