﻿<?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 Greg Goodge  / VARCHAR datatype / 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>Wed, 19 Jun 2013 08:14:48 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Nice and interesting question.</description><pubDate>Wed, 07 Mar 2012 15:15:12 GMT</pubDate><dc:creator>zymos</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Because all colors are in proper case one can be led to believe that case sensitivity is a factor. To avoide this confusion it would be better to have all colors in lower or upper case.</description><pubDate>Tue, 17 Aug 2010 01:03:09 GMT</pubDate><dc:creator>hayk-736382</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Good question! I was shocked to see the four rows when I tried the query!Leaving aside the issue of the COLLATION, I opted for the one row answer, because the first that came to mind was the default value of varchar in the CONVERT function, which is 30.[code="sql"]select convert (varchar, '123456789012345678901234567890123456789012345678901234567890 ')[/code]</description><pubDate>Wed, 28 Jul 2010 05:47:23 GMT</pubDate><dc:creator>dbuendiab</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Good question.  I got it wrong, because I didn't notice that the length wasn't specified.  That will teach me not to skim read code when working out what it does (something I've often told others they must not do, but am still stupid enough to do it myself).</description><pubDate>Mon, 26 Jul 2010 17:43:23 GMT</pubDate><dc:creator>L' Eomot Inversé</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Good question, got it wrong. Didn't know about default length being set to 1 if not declared. Nice job!</description><pubDate>Tue, 20 Jul 2010 10:34:40 GMT</pubDate><dc:creator>Sqlchicken</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Excellent question.  Defaults are always something to keep in mind and to avoid relying on, as they may change.</description><pubDate>Tue, 20 Jul 2010 09:07:59 GMT</pubDate><dc:creator>KevinC.</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Excellent question!  I don't think it's so much about relying on a default (bad practice) so much as forgetting to specify the length, which may not result in a syntax error, and may return results which are incorrect.Thanks for the reality check.</description><pubDate>Tue, 20 Jul 2010 08:31:26 GMT</pubDate><dc:creator>Carla Wilson-484785</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>thanks, this explains everything...:-)[quote][code="sql"]DECLARE @Testvar VARCHAR;SET @Testvar = 'Red';print @Testvar;[/code][/quote]</description><pubDate>Mon, 19 Jul 2010 23:36:06 GMT</pubDate><dc:creator>ziangij</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>I liked this question as well, it demonstrated what many assumed to be one thing but was another, in this case the default length of a varchar when none is specified.</description><pubDate>Mon, 19 Jul 2010 16:49:11 GMT</pubDate><dc:creator>Jan Sorenson</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Good question.  Although the first thing I wanted to know was the collation.</description><pubDate>Mon, 19 Jul 2010 13:10:44 GMT</pubDate><dc:creator>Daniel Bowlin</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>[quote][b]Trey Staker (7/19/2010)[/b][hr]Good Question.  I've been stung by this in production early on in my career.  Since then I always have specified the length of my varchar's.I've read the complaints about coallation.  I always assume default out of the box coallation for QOD but you all have a point since many of you are outside the US and have different coallations.  I think if we took in consideration every tiny thing that we normally assume the question may take pages and pages to set up.[/quote]Thanks - your comment made me wonder why SQL doesn't just throw an error when the varchar definition doesn't include a size. What is the purpose of having two default values based on the kind of declaration vs. CAST and CONVERT, given the possible bad side effects?Just wondering.- webrunner</description><pubDate>Mon, 19 Jul 2010 12:24:56 GMT</pubDate><dc:creator>webrunner</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Excellent question.  Thanks for posting it.</description><pubDate>Mon, 19 Jul 2010 12:19:20 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>[quote][b]Trey Staker (7/19/2010)[/b][hr]I've read the complaints about coallation.  I always assume default out of the box coallation for QOD but you all have a point since many of you are outside the US and have different coallations.  I think if we took in consideration every tiny thing that we normally assume the question may take pages and pages to set up.[/quote]Or like Greg stated, he could have put it all in lower, or upper, case and then the collation wouldn't have mattered. But yes, there are lots of things to think about when writing a QotD to make it successful.</description><pubDate>Mon, 19 Jul 2010 11:17:43 GMT</pubDate><dc:creator>UMG Developer</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Good Question.  I've been stung by this in production early on in my career.  Since then I always have specified the length of my varchar's.I've read the complaints about coallation.  I always assume default out of the box coallation for QOD but you all have a point since many of you are outside the US and have different coallations.  I think if we took in consideration every tiny thing that we normally assume the question may take pages and pages to set up.</description><pubDate>Mon, 19 Jul 2010 11:14:53 GMT</pubDate><dc:creator>Trey Staker</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>This is an excellent question, thank you Greg. It serves as a reminder that "[b]thou shalt not omit the size when declaring a string based variable[/b]" as it can serve as a ground of subtle bugs. For example, int and varchar are implicitly convertible and funny things can happen because of it:[code="sql"]create proc dbo.dump_me(    @input varchar)asbegin    select @input result;end;go[/code]The proc above has a nasty side effect:[code="sql"]exec dbo.dump_me '123';[/code]produces[code="sql"]result------1[/code]However, removing the single quotes:[code="sql"]exec dbo.dump_me 123;[/code]produces[code="sql"]result------*[/code]While this behaviour is by design, it might catch someone by surprise. The size is omitted, so '123' becomes [b]1[/b] when cast to varchar(1), but implementation of int data type allows returning [b]*[/b] when the number of characters in the int does not fit.Oleg</description><pubDate>Mon, 19 Jul 2010 09:56:49 GMT</pubDate><dc:creator>Oleg Netchaev</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>[quote][b]Kanaka (7/19/2010)[/b][hr]I am not sure why sql server 2008 returned 1, inspite of not giving the variable length.I knew this would give a syntax error, however, I tried this and it returned 1 row.:w00t:[/quote]The version of SQL Server doesn't matter.  The only way that you will get one record is if you are using a case-sensitive collation.Thanks,Greg</description><pubDate>Mon, 19 Jul 2010 09:22:40 GMT</pubDate><dc:creator>JestersGrind</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>I am not sure why sql server 2008 returned 1, inspite of not giving the variable length.I knew this would give a syntax error, however, I tried this and it returned 1 row.:w00t:</description><pubDate>Mon, 19 Jul 2010 09:04:38 GMT</pubDate><dc:creator>Kanaka</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>A lesson that's common to many QotDs is that you should never rely on default values - always be specific, so you know exactly what you're getting, and so does naybody else who reads the code later!A lesson which I break whenever I create a table without specifying 'primary' all over the place ;-)</description><pubDate>Mon, 19 Jul 2010 08:52:43 GMT</pubDate><dc:creator>Toreador</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Good question. In this case I don't consider it a trick question because the lack of a length specification in the statement "DECLARE @TestVar VARCHAR" pointed to behavior based on omitting the length. At that point there is not so much trickery as a real test of knowledge of how SQL behaves in the absence of a length specification. That's a valid test of knowledge, not a trick.I got the question wrong, but that is as it should be because I didn't know the information the question was testing for. I learned something from the question and the documentation, namely: [quote]When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified when using the CAST and CONVERT functions, the default length is 30.[/quote]http://msdn.microsoft.com/en-us/library/ms176089.aspxI assumed the length is always 30 when a length is not specified, but as the documentation states, it is sometimes 1. That's enough to make the question a valuable one, as far as I am concerned.Thanks,webrunner</description><pubDate>Mon, 19 Jul 2010 08:20:57 GMT</pubDate><dc:creator>webrunner</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>I too thought the default length was 30.Not stating the collation setting makes it obvious no matter what actual db settings you work with regularly.Thanks for the question and education.</description><pubDate>Mon, 19 Jul 2010 07:50:04 GMT</pubDate><dc:creator>Tom Garth</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Thanks for all of the feedback.  I definitely should have considered case-sensitivity, especially since we have several case-sensitive database here.  In hind sight, I should have made everything lower case to avoid confusion.  A little back story...  I ran into this situation while reviewing a developer's stored procedure that was returning more results than it should have.  Up until that point, I had never needed to know the default length of a varchar variable, because I always followed the best practice of explicitly stating the length of a varchar variable.  I guess this is why it's a best practice.Thanks,Greg</description><pubDate>Mon, 19 Jul 2010 07:23:35 GMT</pubDate><dc:creator>JestersGrind</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Nice question - I guessed that the collation was case insensitive (the default on any installation I've done), but got it wrong anyway, because I didn't spot that "Green" had an "r" in it. I guess that's Mondays for you.Duncan</description><pubDate>Mon, 19 Jul 2010 07:17:46 GMT</pubDate><dc:creator>Duncan Pryde</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Nice question.I had to browse my memory from long ago after I saw that VARCHAR declaration. I had forgotten that.Thanks a lot. :cool:</description><pubDate>Mon, 19 Jul 2010 07:04:45 GMT</pubDate><dc:creator>codebyo</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>[quote][b]SanjayAttray (7/18/2010)[/b][hr]Nice question.When the length is not specified in a data definition or variable declaration statement, the default length is 1 ----- Didn't knew this.  used to give length to all variables.[/quote]And I really hope that you continue to give length to all variables. Relying on this default length is bad practice. And very confusing, since the default length depends on where the data type is used - in a CONVERT or CAST function, the default is 30; elsewhere it is 1.</description><pubDate>Mon, 19 Jul 2010 04:41:52 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>A good question about a common misunderstanding. The number of times when a problem posted to usenet or forums could be solved by simply adding the length to a varchar parameter is staggering.My only minor gripe is, as already noted by others, the lack of collate indication. On my default test server, this query would have returned 1 row, as I use a case sensitive collation there. Fortunately I assumed out of the box default settings for the instance, so I replied 4 and got it right.Thanks for the great question; I'm looking forward to seeing more of these, Greg!</description><pubDate>Mon, 19 Jul 2010 04:40:31 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Hmmmm, it appears I don't have much of a point, but, if I did, it would be that it is not good practice to cast a variable unless it is strongly typed.   select * from( select 'red' textfieldunion select 'blue' textfieldunion select 'green' textfieldunion select 'yellow' textfieldunion select 'orange' textfieldunion select 'purple' textfield)xwhere convert(varchar(1),textfield) like '%red%' ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_6tsql/html/282cd982-f4fb-4b22-b2df-9e8478f13f6a.htmWhen n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified when using the CAST and CONVERT functions, the default length is 30.[quote]Not 100% sure but I think the rule of default length applies only to variable declaration; functions have other rules: CONVERT(VARCHAR, expression) defaults to VARCHAR(30).[/quote]But, I was unaware that the varchar declaration has a default.  Normally, I wouldn't think of trying this so now the question has forced me to think outside the box.  To wit, perhaps it is a good question after all.</description><pubDate>Mon, 19 Jul 2010 01:06:52 GMT</pubDate><dc:creator>Jamie Longstreet-481950</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Nice question.I did not notice the variable length.that's why i got wrong.I had answered 1.</description><pubDate>Mon, 19 Jul 2010 00:31:23 GMT</pubDate><dc:creator>malleswarareddy_m</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Nice question!!</description><pubDate>Mon, 19 Jul 2010 00:08:02 GMT</pubDate><dc:creator>udayroy15</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>I agree, the collation setting (whether it's case sensitive or not) would bring different results (1 for case-sensitive and 4 for case-insensitive).</description><pubDate>Sun, 18 Jul 2010 16:18:39 GMT</pubDate><dc:creator>Iggy-SQL</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>This could make the Q "less confusing" but less real-life and interesting:[code="sql"]DECLARE @Testvar VARCHAR;SET @Testvar = 'Red';print @Testvar;[/code]If only there was a remark of the collation set (as UMG developer pointed out) to avoid ambiguity.</description><pubDate>Sun, 18 Jul 2010 15:11:41 GMT</pubDate><dc:creator>hrvoje.piasevoli</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>[quote][b]Jamie Longstreet-481950 (7/18/2010)[/b][hr]Textfield is the field name... are we not saying the same thing?  The textfield is not the same for both the variable and the temp test table.  If you remove the temp table and use a sub-query, it makes the issue less confusing. [/quote]I don't understand what you are trying to say. Yes Textfield is the name, but it has nothing to do with the variable, and your example isn't at all what the question is about.The issue is the "DECLARE @Testvar VARCHAR" and knowing that that means the same as "DECLARE @Testvar VARCHAR(1)" and that the SET truncates the data and does not report an error. I'm not sure why you think that is confusing.</description><pubDate>Sun, 18 Jul 2010 14:45:48 GMT</pubDate><dc:creator>UMG Developer</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Both 1 and 4 could be the correct answer as Green, Orange and Purple do not actually contain a "R", so if you have a case sensitive collation you would only have one row returned. But I figured that wasn't you were looking for, and "It depends" wasn't a choice. ;-)In any case, nice question, thanks!</description><pubDate>Sun, 18 Jul 2010 14:40:46 GMT</pubDate><dc:creator>UMG Developer</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Textfield is the field name... are we not saying the same thing?  The textfield is not the same for both the variable and the temp test table.  If you remove the temp table and use a sub-query, it makes the issue less confusing.   You'll excuse me... I'm griping.  I don't like the way the question was worded.  Should have said that in the first place.</description><pubDate>Sun, 18 Jul 2010 13:22:05 GMT</pubDate><dc:creator>Jamie Longstreet-481950</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>[quote][b]Jamie Longstreet-481950 (7/17/2010)[/b][hr]Could call it a trick... in another form:select * from(select 'red' union select 'red'union select 'blue'union select 'green'union select 'yellow'union select 'orange'union select 'purple')xwhere textfield like 'red'  -- or like '%red%' same thingIn the above form, it's obvious we have no field named "textfield".[/quote]Don't see how this relates to the posted question. No trick in the question just default behavior for unspecified optional parameter.</description><pubDate>Sun, 18 Jul 2010 12:56:36 GMT</pubDate><dc:creator>hrvoje.piasevoli</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Nice question. I got it right cause I made an effort to examine all kinds of default parameters after encountering something similar. Not 100% sure but I think the rule of default length applies only to variable declaration; functions have other rules: CONVERT(VARCHAR, expression) defaults to VARCHAR(30).My 2cRegards,Hrvoje</description><pubDate>Sun, 18 Jul 2010 12:51:34 GMT</pubDate><dc:creator>hrvoje.piasevoli</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Nice question.When the length is not specified in a data definition or variable declaration statement, the default length is 1 ----- Didn't knew this.  used to give length to all variables.</description><pubDate>Sun, 18 Jul 2010 07:25:54 GMT</pubDate><dc:creator>SanjayAttray</dc:creator></item><item><title>RE: VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Could call it a trick... in another form:select * from(select 'red' union select 'red'union select 'blue'union select 'green'union select 'yellow'union select 'orange'union select 'purple')xwhere textfield like 'red'  -- or like '%red%' same thingIn the above form, it's obvious we have no field named "textfield".</description><pubDate>Sat, 17 Jul 2010 16:38:50 GMT</pubDate><dc:creator>Jamie Longstreet-481950</dc:creator></item><item><title>VARCHAR datatype</title><link>http://www.sqlservercentral.com/Forums/Topic954419-2742-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/questions/T-SQL/70321/"&gt;VARCHAR datatype&lt;/A&gt;[/B]</description><pubDate>Sat, 17 Jul 2010 12:17:27 GMT</pubDate><dc:creator>JestersGrind</dc:creator></item></channel></rss>