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 12»»

CASTing Expand / Collapse
Author
Message
Posted Wednesday, May 28, 2008 10:41 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:49 AM
Points: 2,620, Visits: 2,467
Comments posted to this topic are about the item CASTing
Post #508168
Posted Thursday, May 29, 2008 1:21 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:56 AM
Points: 5,487, Visits: 10,335
Sorry, I don't understand. What CAST to nvarchar(800)? Is there a typo in the question?

John
Post #508216
Posted Thursday, May 29, 2008 8:13 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Tuesday, December 2, 2014 8:26 AM
Points: 775, Visits: 1,203
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
Post #508413
Posted Thursday, May 29, 2008 8:25 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, May 30, 2014 6:27 PM
Points: 2,808, Visits: 7,175
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.
Post #508428
Posted Thursday, May 29, 2008 8:36 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Tuesday, December 2, 2014 8:26 AM
Points: 775, Visits: 1,203
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
Post #508439
Posted Thursday, May 29, 2008 8:41 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, May 30, 2014 6:27 PM
Points: 2,808, Visits: 7,175
nvarchar has a max length of 4000 not 8000
the 'N' in this statment converts it;

set @c = N'hello'

Post #508444
Posted Thursday, May 29, 2008 8:47 AM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, December 15, 2014 10:28 AM
Points: 1,544, Visits: 2,274
Good question of the day...I totally fell for it!

The Redneck DBA
Post #508449
Posted Thursday, May 29, 2008 8:49 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:56 AM
Points: 5,487, Visits: 10,335
Yeah, so did I. I see what happened now.

John
Post #508451
Posted Thursday, May 29, 2008 9:30 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, July 13, 2011 2:21 AM
Points: 251, 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
Post #508481
Posted Thursday, May 29, 2008 2:01 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, October 7, 2014 2:56 AM
Points: 2,842, Visits: 3,876
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
Post #508689
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse