Printing Lengths

  • Rahul The Dba (12/13/2010)


    thanks

    n i promise i will keep ur words in mind, nxt time i post some questions..

    Rahul,

    We are not able to tell who these comments are for if you do not post a quoted reply like this one.

    😎 Or at the very least include thier name in the post.

    --------

    I have.

    Who has ever been called a "Top Poster"?

  • Thanks for the interesting question. Though I hope questions like this wouldn't be necessary, and that people would always specify a length on variables to keep themselves out of trouble.

  • Thanks for the question.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • good one, thanks Rahul...:-)

    declare @myvariable as varchar, @len1 varchar, @len2 varchar

    set @myvariable = 'hi hello how are you'

    set @len1 = (select datalength(cast(@myvariable as char)))

    print @len1

    set @len1 = (select datalength(cast(@myvariable as varchar)))

    print @len1

    set @len2 = (select datalength(convert(char, @myvariable)))

    print @len2

    set @len2 = (select datalength(convert(varchar, @myvariable)))

    print @len2

    The above code generates output as

    *

    1

    *

    1

    difference of char, varchar datatypes...:-)

  • Nice question. Learn something new today...

    Thanks

  • Hardy21 (12/14/2010)


    Nice question. Learn something new today...

    thankss

    [font="Comic Sans MS"]Rahul:-P[/font]

  • This is an old kind of COBOL bug:

    using bad datatype to hold integer:

    01 IDX PIC 9.

    PERFORM LBL VARYING IDX BY 1 UNTIL IDX > 10.

    Endless loop

  • Thanks for the question, Rahul.

    But personally I find the explanation somewhat confusing, specifically the last paragraph where it reads:

    It is '*' because when we convert character or binary expressions to an expression of a different data type, data can be truncated, only partially displayed, or an error is returned because the result is too short to display. * means the result length too short to display

    In regards to the default length when n is undefined, it doesn't make a difference whether you use CAST or CONVERT.

    @length1 prints 1 because no conversion is carried out (varchar = varchar).

    @length2 prints * because the result (30, due to the conversion from varchar to char) actually is too long to fit into a column implicitely set to varchar(1) = by not specifying the length in the variable declarartion.

    Regards,

    Michael

  • I took a stab at 1,30. So it was 1,*. Ha, very good. I'll have forgotten this again in half an hour, and I still won't care 😀

  • SQLkiwi (12/18/2010)


    I took a stab at 1,30. So it was 1,*. Ha, very good. I'll have forgotten this again in half an hour, and I still won't care 😀

    Will you have forgotten it again because you always specify the length and this isn't an issue? (Unless of course you are working on code written by somebody that doesn't.)

  • UMG Developer (12/18/2010)


    Will you have forgotten it again because you always specify the length and this isn't an issue? (Unless of course you are working on code written by somebody that doesn't.)

    Yes. If I do have to work on code written by someone that thought it was clever to omit lengths, I always have to look it up to be sure.

  • Thanks for the question! Learned something today. I knew about the default length for char/varchar, but not the convert issue. An important sample to show why you shouldn't omit default values or on mandatory statements.

    /HÃ¥kan Winther
    MCITP:Database Developer 2008
    MCTS: SQL Server 2008, Implementation and Maintenance
    MCSE: Data Platform

  • Thanks for the question on the data types.

Viewing 13 posts - 16 through 28 (of 28 total)

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