﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Article Discussions / Article Discussions by Author / Discuss content posted by SveG  / Data Type and Length / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 12:28:28 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>[quote][b]David Data (9/27/2010)[/b][hr]But I am curious as to why a VARCHAR(MAX) string that happens to contain say 100 chars is so much slower that a VARCHAR(100) that does?  Its index and length values will have to be 32 bit numbers, but with 32/64 bit CPUs anything shorter tends to be less rather than more efficient anyway.  As even a VARCHAR(100) is stored as a variable length string, I would not imagine the memory management issues are much different either.  Or are they?[/quote]Im not sure.  Some folks insist (and maybe rightfully so... I seem to remember such a thing in BOL but don't remember for sure) the VARCHAR(MAX) stays "inrow" under such conditions.  To me, it seems like it may not but  I've not taken the time to research that to any depth.  It would be nice to know for sure but I just don't have the floor space on my dance card right now.</description><pubDate>Thu, 30 Sep 2010 22:05:18 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>[quote][b]David Data (9/28/2010)[/b][hr]OTOH I designed our ETL system with [Postcode] [nvarchar](10) and then had to change it when I found records with postcodes like 'If out please leave by back door or with neighbour at No. 127' :([/quote]I really hope your design change was to add a column for "delivery comments", not to increase the maximum length for the postcode. (Unless your business operates in a country where 'If out please leave by back door or with neighbour at No. 127' is a valid postcode).But I do hope you changed the postcode to varchar(10). As far as I know, all countries in the world that use postal codes limit them to numbers and the letters A-Z.</description><pubDate>Tue, 28 Sep 2010 03:02:01 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>OTOH I designed our ETL system with [Postcode] [nvarchar](10) and then had to change it when I found records with postcodes like 'If out please leave by back door or with neighbour at No. 127' :([quote][b]magasvs (9/27/2010)[/b][hr]The problem with wide columns is that even though SQL Server supports now larger row size the maximum capacity specification is still 8,060 bytes per row. If the row becomes wider - then row overflow occurs and data is saved differently.[/quote]I understand the need to keep the actual data short, (especially for fields in the primary key, where I prefer to use INT or CHAR if possible), but does a record containing a couple of VARCHAR(8000)'s or even a VARCHAR(MAX) which happen to contain 50-char strings need row overflow, or does it only need 100 bytes (+overhead)?  My tests suggest the latter.</description><pubDate>Tue, 28 Sep 2010 02:04:56 GMT</pubDate><dc:creator>David Data</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>[quote][b]magasvs (9/27/2010)[/b][hr]There were columns like Year nvarchar (255), PostalCode nvarchar (255), Phone nvarchar (8000)...[/quote]One of our developers stored a TCP port number (which is, by definition, an unsigned 16-bit integer) in a VARCHAR(50) field...sometimes I despair. ;-)</description><pubDate>Tue, 28 Sep 2010 01:29:11 GMT</pubDate><dc:creator>paul.knibbs</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>The problem with wide columns is that even though SQL Server supports now larger row size the maximum capacity specification is still 8,060 bytes per row. If the row becomes wider - then row overflow occurs and data saved differently. This affects query performance (I/O performance degradation). More details are here http://msdn.microsoft.com/en-us/library/ms186981.aspx. This works differently with varchar(max), nvarchar(max), varbinary(max), text, image, or xml data types, but I just wanted to make point that columns width should be reasonable. Another example of incorrect data type usage is using NVARCHAR or FLOAT data types when it's not required. Just a couple of days ago I have been testing large database migration and by changing NVARCHAR, DATETIME to VARCHAR and DATE/SMALLDATETIME (together with column width reduction) one of the tables reduced in size by 11 GB! There were columns like Year nvarchar (255), PostalCode nvarchar (255), Phone nvarchar (8000)...</description><pubDate>Mon, 27 Sep 2010 19:14:17 GMT</pubDate><dc:creator>magasvs</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>(As someone has re-awakened this subject...)[quote][b]Jeff Moden (8/23/2010)[/b][hr]There is sometimes a HUGE performance penalty (2:1) to be paid for using VARCHAR(MAX) depending, of course, on what you're doing.  Correct sizing is always important.  I'd dare say the same would be true for most any language... even .Net.[/quote]I was talking about local scalar variables, not arrays or database fields where extra microseconds can get multiplied by millions of records.But I am curious as to why a VARCHAR(MAX) string that happens to contain say 100 chars is so much slower that a VARCHAR(100) that does?  Its index and length values will have to be 32 bit numbers, but with 32/64 bit CPUs anything shorter tends to be less rather than more efficient anyway.  As even a VARCHAR(100) is stored as a variable length string, I would not imagine the memory management issues are much different either.  Or are they?That said, I do always use the smallest VARCHAR I can - though I'm not sure whether it really makes much difference as SQL Server seems to allocate space for the actual content not the potential maximum content, as I tested by creating a large table of VARCHAR(4096) containing just 'x' (1 character) per record.</description><pubDate>Mon, 27 Sep 2010 16:55:09 GMT</pubDate><dc:creator>David Data</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>Great Question it chew up my hour worth ups time. :-D</description><pubDate>Sun, 26 Sep 2010 23:30:53 GMT</pubDate><dc:creator>Dhruvesh Shah</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>[quote][b]paul.knibbs (8/23/2010)[/b][hr][quote]There is sometimes a HUGE performance penalty (2:1) to be paid for using VARCHAR(MAX) depending, of course, on what you're doing.  Correct sizing is always important.  I'd dare say the same would be true for most any language... even .Net.[/quote]Well, the person I was responding to never mentioned anything about performance, he just wanted a text field that can be any length in T-SQL, and I was pointing out that already exists. :-)[/quote]Heh... Well, everyone should always be concerned about performance so I was pointing out how bad it can actually be when you don't size stuff properly. :-)</description><pubDate>Mon, 23 Aug 2010 21:09:28 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>[quote]There is sometimes a HUGE performance penalty (2:1) to be paid for using VARCHAR(MAX) depending, of course, on what you're doing.  Correct sizing is always important.  I'd dare say the same would be true for most any language... even .Net.[/quote]Well, the person I was responding to never mentioned anything about performance, he just wanted a text field that can be any length in T-SQL, and I was pointing out that already exists. :-)</description><pubDate>Mon, 23 Aug 2010 05:16:39 GMT</pubDate><dc:creator>paul.knibbs</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>[quote][b]paul.knibbs (8/11/2010)[/b][hr][quote][b]David Data (8/10/2010)[/b]It would be nice to have a datatype like .NET's [b]string[/b] which can carry strings of (almost) any length without worrying about what its contents' maximum length might happen to be.  Fields in database tables may need to be specified precisely; local variables should not.[/quote]Isn't that pretty much what VARCHAR(MAX) is?[/quote]There is sometimes a HUGE performance penalty (2:1) to be paid for using VARCHAR(MAX) depending, of course, on what you're doing.  Correct sizing is always important.  I'd dare say the same would be true for most any language... even .Net.</description><pubDate>Mon, 23 Aug 2010 05:14:13 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>[quote][b]Hugo Kornelis (8/9/2010)[/b][hr]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![/quote]Nah... it just shows that 47% of the people are too lazy to run the code which is also why these are only worth 1 point. :-P</description><pubDate>Mon, 23 Aug 2010 05:09:41 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>Good question! Thanks.</description><pubDate>Wed, 11 Aug 2010 06:47:29 GMT</pubDate><dc:creator>VM-723206</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>[quote][b]David Data (8/10/2010)[/b]It would be nice to have a datatype like .NET's [b]string[/b] which can carry strings of (almost) any length without worrying about what its contents' maximum length might happen to be.  Fields in database tables may need to be specified precisely; local variables should not.[/quote]Isn't that pretty much what VARCHAR(MAX) is?</description><pubDate>Wed, 11 Aug 2010 01:01:28 GMT</pubDate><dc:creator>paul.knibbs</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>I got it wrong too.  But like many questions here, the answer depends on knowing the many inconsistencies and strange rules in SQL Server - most of which you never need to know.I had no idea how long char and varchar default sizes are, for the simple reason that I've never declared any without specifying the length. IMHO the correct answer [i]should [/i]have been 'Syntax Error'!It would be nice to have a datatype like .NET's [b]string[/b] which can carry strings of (almost) any length without worrying about what its contents' maximum length might happen to be.  Fields in database tables may need to be specified precisely; local variables should not.</description><pubDate>Tue, 10 Aug 2010 13:41:53 GMT</pubDate><dc:creator>David Data</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>Good question. Thank you!</description><pubDate>Mon, 09 Aug 2010 17:56:19 GMT</pubDate><dc:creator>Kangana Beri</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>This is a very good question.  My initial choice was 3/38.  It's a good thing I decided to run the code before answering.  I learned something new :-) ...</description><pubDate>Mon, 09 Aug 2010 15:01:24 GMT</pubDate><dc:creator>Enigma475</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>Thanks very much!  This is a question that applies to the category of "things I need to know and will probably run across" and the answer is straight-forward/simple!  Ok, I got it wrong, but that's because I have plenty to learn.(The last couple times I've commented, it was due to being frustrated with the question, thought I might add some positive feedback as well :cool:)</description><pubDate>Mon, 09 Aug 2010 09:04:37 GMT</pubDate><dc:creator>pjdiller</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>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. ;-)</description><pubDate>Mon, 09 Aug 2010 08:02:08 GMT</pubDate><dc:creator>Sean Lange</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>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.</description><pubDate>Mon, 09 Aug 2010 07:43:01 GMT</pubDate><dc:creator>Daniel Bowlin</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>[p]Good question. Thank you.[/p]</description><pubDate>Mon, 09 Aug 2010 07:22:34 GMT</pubDate><dc:creator>Kari Suresh</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>Great Question. Thank you.</description><pubDate>Mon, 09 Aug 2010 06:45:04 GMT</pubDate><dc:creator>Dennissinned</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>Very good question indeed. Takes back to basics :-)</description><pubDate>Mon, 09 Aug 2010 04:25:38 GMT</pubDate><dc:creator>Ameya- Ameyask</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>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!</description><pubDate>Mon, 09 Aug 2010 03:54:38 GMT</pubDate><dc:creator>paul.knibbs</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>Good question![quote][b]Hugo Kornelis (8/9/2010)[/b][hr]...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. [/quote]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</description><pubDate>Mon, 09 Aug 2010 02:24:31 GMT</pubDate><dc:creator>hrvoje.piasevoli</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>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![quote][b]Bhuvnesh (8/9/2010)[/b][hr]30 is for char or varchar ?[/quote]Both, when used without length in a CONVERT() function call.In a DECLARE, both char and varchar default to a length of 1.</description><pubDate>Mon, 09 Aug 2010 00:59:05 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>30 is for char or varchar ?</description><pubDate>Mon, 09 Aug 2010 00:17:14 GMT</pubDate><dc:creator>Bhuvnesh</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>Great question.   As the others have said, this underlines the necessity of specifying the size of the datatype.</description><pubDate>Sun, 08 Aug 2010 22:51:43 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>[quote][b]Oleg Netchaev (8/7/2010)[/b][hr]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[/quote]You're right. Trusting default behaviours should be avoided at all costs.But 12 lashes is too soft. :-D</description><pubDate>Sat, 07 Aug 2010 23:08:35 GMT</pubDate><dc:creator>codebyo</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>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.)</description><pubDate>Sat, 07 Aug 2010 22:59:33 GMT</pubDate><dc:creator>UMG Developer</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>[quote][b]codebyo (8/7/2010)[/b][hr]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?[/quote]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</description><pubDate>Sat, 07 Aug 2010 20:11:31 GMT</pubDate><dc:creator>Oleg Netchaev</dc:creator></item><item><title>RE: Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>Thanks for question. I just lost one point. :-DNow 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?</description><pubDate>Sat, 07 Aug 2010 13:04:00 GMT</pubDate><dc:creator>codebyo</dc:creator></item><item><title>Data Type and Length</title><link>http://www.sqlservercentral.com/Forums/Topic965579-1276-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/T-SQL/70497/"&gt;Data Type and Length&lt;/A&gt;[/B]</description><pubDate>Sat, 07 Aug 2010 13:00:57 GMT</pubDate><dc:creator>magasvs</dc:creator></item></channel></rss>