Data Type and Length

  • Comments posted to this topic are about the item Data Type and Length

  • Thanks for question. I just lost one point. 😀

    Now why in Heavens would there be two different behaviours for the same datatype?

    Just to confuse people? Or is there a technical explanation for that?

    Best regards,

    Andre Guerreiro Neto

    Database Analyst
    http://www.softplan.com.br
    MCITPx1/MCTSx2/MCSE/MCSA

  • codebyo (8/7/2010)


    Now why in Heavens would there be two different behaviours for the same datatype?

    Just to confuse people? Or is there a technical explanation for that?

    This is a very good question, I really like it.

    I think that this data type size behaviour is great. Since there is no way in SQL Server to punish the disobedient by, say, 12 lashes they deserve, this difference is actually a good way to teach them to never omit the size when declaring / converting variables. On the top of the 1 / 30 issue, there is another interesting twist to it: both ADODB and ADO.NET default the size of the varchar type procedure parameter to 50 if the size is not specified. The bottom line is that forgetting to specify the size is evil and should be avoided.

    As far as a technical explanation is concerned, I believe that because the size is required, default values were provided by the parser team, and it probably just so happened that the declaration and conversion were written by different developers, that is all.

    Oleg

  • Great question thanks!

    This is a very good topic to cover, as I know a lot of developers don't understand the importance of specifying the size. (Good old VB "String" type.)

  • Oleg Netchaev (8/7/2010)


    I think that this data type size behaviour is great. Since there is no way in SQL Server to punish the disobedient by, say, 12 lashes they deserve, this difference is actually a good way to teach them to never omit the size when declaring / converting variables. On the top of the 1 / 30 issue, there is another interesting twist to it: both ADODB and ADO.NET default the size of the varchar type procedure parameter to 50 if the size is not specified. The bottom line is that forgetting to specify the size is evil and should be avoided.

    As far as a technical explanation is concerned, I believe that because the size is required, default values were provided by the parser team, and it probably just so happened that the declaration and conversion were written by different developers, that is all.

    Oleg

    You're right. Trusting default behaviours should be avoided at all costs.

    But 12 lashes is too soft. 😀

    Best regards,

    Andre Guerreiro Neto

    Database Analyst
    http://www.softplan.com.br
    MCITPx1/MCTSx2/MCSE/MCSA

  • Great question. As the others have said, this underlines the necessity of specifying the size of the datatype.

    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

  • 30 is for char or varchar ?

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • Good question! When I first saw it, I thought there would be a very high percentage of correct answers since the confusing default lengths for character strings have already been covered in a few recent QotD's. But the results say that at this time, only 53% of the respondents have given the right answer, so there is obviously still a lot of need to keep driving this point home. Good job, magasvs!

    Bhuvnesh (8/9/2010)


    30 is for char or varchar ?

    Both, when used without length in a CONVERT() function call.

    In a DECLARE, both char and varchar default to a length of 1.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Good question!

    Hugo Kornelis (8/9/2010)


    ...I thought there would be a very high percentage of correct answers since the confusing default lengths for character strings have already been covered in a few recent QotD's.

    I guess people don't go through discussions. (July, 19th QotD only asked for the implicit length of variable declaration while the CONVERT behavior was mentioned and explained in the discussion that followed.)

    Regards,

    Hrvoje

    Hrvoje Piasevoli

  • Completely forgot about the CONVERT when answering this, but probably would have got it wrong even if I'd taken it into account...didn't know about this default length behaviour!

  • Very good question indeed. Takes back to basics 🙂

  • Great Question. Thank you.

  • [p]Good question. Thank you.[/p]

    KSB
    -----------------------------
    Thousands of candles can be lit from a single candle, and the life of the candle will not be shortened. Knowledge and happiness never decreases by being shared.” - Buddha

  • Curious, the difference in default behaviour between a DECLARE and a CONVERT.

    I suppose this just puts another check mark in the Always Declare Everything column.

  • Excellent question! I noticed the lack of defining the size and my initial response was 1;1 but since that was not a choice I figured there must be something in convert for default sizes. Had there been an answer of 1;1 I would have assumed that was the correct answer. 😉

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 15 posts - 1 through 15 (of 31 total)

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