Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Data Type and Length


Data Type and Length

Author
Message
magasvs
magasvs
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1285 Visits: 752
Comments posted to this topic are about the item Data Type and Length
Andre Guerreiro
Andre Guerreiro
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1065 Visits: 1514
Thanks for question. I just lost one point. :-D

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
Oleg Netchaev
Oleg Netchaev
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1693 Visits: 1807
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
UMG Developer
UMG Developer
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2280 Visits: 2204
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.)
Andre Guerreiro
Andre Guerreiro
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1065 Visits: 1514
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. :-D

Best regards,

Andre Guerreiro Neto

Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2/MCSE/MCSA
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21067 Visits: 18258
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

Bhuvnesh
Bhuvnesh
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2924 Visits: 4076
30 is for char or varchar ?

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)SSCrazy Eights (8.3K reputation)

Group: General Forum Members
Points: 8317 Visits: 11543
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 MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
hrvoje.piasevoli
hrvoje.piasevoli
Old Hand
Old Hand (365 reputation)Old Hand (365 reputation)Old Hand (365 reputation)Old Hand (365 reputation)Old Hand (365 reputation)Old Hand (365 reputation)Old Hand (365 reputation)Old Hand (365 reputation)

Group: General Forum Members
Points: 365 Visits: 510
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
paul.knibbs
paul.knibbs
SSCommitted
SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)SSCommitted (2K reputation)

Group: General Forum Members
Points: 1972 Visits: 6213
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!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search