Strings and Defaults

  • nice question.

    Evey time I recall that default varchar length is 1 for declare but 30 for cast I feel sick and resolve never to use varchar without specifying the length.

    Tom

  • I knew that second variable would hold all 50 characters just didn't realize that case as varchar without a length would default to 30. Makes sense. I should have realized that.

  • I want to side with those who find this inconsistency as unacceptable. I can live with defaults but in this case there are 2, 1 and 30. There should be one default or every varchar should have to specify a length. Maybe a Server or Database option.

    It was a good question but you shouldn't have to know these arcane default values

  • Good Question, Thanks Gail.

  • David Conn (2/1/2012)


    It was a good question but you shouldn't have to know these arcane default values

    Personally my opinion is that you shouldn't have to know the defaults because you never ever declare varchar/char/nvarchar/nchar/binary/varbinary/etc without specifying a length.

    I would personally like to see declaring one of those data types without specifying a length deprecated. Won't happen though.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • GilaMonster (2/1/2012)


    David Conn (2/1/2012)


    It was a good question but you shouldn't have to know these arcane default values

    Personally my opinion is that you shouldn't have to know the defaults because you never ever declare varchar/char/nvarchar/nchar/binary/varbinary/etc without specifying a length.

    I would personally like to see declaring one of those data types without specifying a length deprecated. Won't happen though.

    If you raise it as a suggestion on connect I'll vote for it.

    Tom

  • Question is, how far do you go?

    Just char, varchar, nchar, nvarchar, binary, varbinary?

    Also decimal and numeric (default is numeric(18,0))?

    Also time (default is time(7)), datetime2 (default is datetime2(7)) and datetimeoffset (default is datetimeoffset(7))?

    The new datetime and time default to their highest precision. Numeric and decimal default to something in the middle. The character and binary types default to their minimum when declared.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • I lay my vote for that declaration of varchar/char/nvarchar/nchar/binary/varbinary/etc data types without specifying length should throw an error (or at least warning) by the parser. Anyone, lead me and I will follow :-). Maybe this issue is already raised at Microsoft ...

  • GilaMonster (2/1/2012)


    Question is, how far do you go?

    Just char, varchar, nchar, nvarchar, binary, varbinary?

    Also decimal and numeric (default is numeric(18,0))?

    Also time (default is time(7)), datetime2 (default is datetime2(7)) and datetimeoffset (default is datetimeoffset(7))?

    The new datetime and time default to their highest precision. Numeric and decimal default to something in the middle. The character and binary types default to their minimum when declared.

    I want to go all the way :-). Thanks Gail for adding these. Everywhere were length or precision can be specified. Maybe, a warning message would be the most elegant and a good compromise.

    Cheers

    Istvan

  • GilaMonster (2/1/2012)


    Question is, how far do you go?

    Just char, varchar, nchar, nvarchar, binary, varbinary?

    Also decimal and numeric (default is numeric(18,0))?

    Also time (default is time(7)), datetime2 (default is datetime2(7)) and datetimeoffset (default is datetimeoffset(7))?

    The new datetime and time default to their highest precision. Numeric and decimal default to something in the middle. The character and binary types default to their minimum when declared.

    I think the default precisions for time, datetime2 and datetimeoffset are sensible, so although I have a general feeling that precision defaults are not a good thing I guess I can live with the defaults for those three.

    Decimal and Numeric have such bizarre rounding and precision/scale adjustment conventions that it seems pure insanity to have defaults for precision and scale, since the effects could be disastrous; so I'd prefer it to be forbidden to omit precision and scale for these types (actually I'd prefer MS to implement the 2008 revision of the floating point standard so that we could have exponents to base 10 and deprecate decimal, numeric, money and smallmoney, but there's no chance of that in the short or medium term, and probably very little chance even in the long term).

    Tom

  • Brigadur (2/1/2012)


    Maybe, a warning message would be the most elegant and a good compromise.

    Cheers

    Istvan

    I'd prefer to have a configuration option that determines whether it's a warning message or an error (default being error).

    Tom

  • L' Eomot Inversé (1/26/2012)


    nice question.

    Evey time I recall that default varchar length is 1 for declare but 30 for cast I feel sick and resolve never to use varchar without specifying the length.

    Nice new avatar, Tom.

  • L' Eomot Inversé (2/1/2012)


    Brigadur (2/1/2012)


    Maybe, a warning message would be the most elegant and a good compromise.

    Cheers

    Istvan

    I'd prefer to have a configuration option that determines whether it's a warning message or an error (default being error).

    That's a good idea! With the warning option, i was thinking of all existing code out there (not mine of course 😉 ) that relies on defaults.

  • +1

  • Brigadur (2/1/2012)


    L' Eomot Inversé (2/1/2012)


    That's a good idea! With the warning option, i was thinking of all existing code out there (not mine of course 😉 ) that relies on defaults.

    +1.

    Error messages could break existing code but there could be a session SET option that could possibly show warnings to most common coding mistakes perhaps. Like some recommended guidelines. But with advices from people here, not only the SQL Server team (who love procedural coding sometimes).

    And profiler has two events for catching deprecated code. Maybe a few more events there? 🙂

    Best regards,

    Andre Guerreiro Neto

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

Viewing 15 posts - 31 through 45 (of 47 total)

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