CASTing

  • Comments posted to this topic are about the item CASTing

  • Sorry, I don't understand. What CAST to nvarchar(800)? Is there a typo in the question?

    John

  • 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.

    SQLServerNewbieMCITP: Database Administrator SQL Server 2005
  • 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.

  • 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.

    SQLServerNewbieMCITP: Database Administrator SQL Server 2005
  • nvarchar has a max length of 4000 not 8000

    the 'N' in this statment converts it;

    set @C = N'hello'

  • Good question of the day...I totally fell for it!

    The Redneck DBA

  • Yeah, so did I. I see what happened now.

    John

  • 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

  • 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

  • Thanks for the clarification. 😀

  • 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

    I totally agree after thinking about these two QOD (great ones, btw). Is there a way to enable explicit (strict) conversions when writing stored procedures in SQL 2005?

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply