Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««1234»»»

VARCHAR datatype Expand / Collapse
Author
Message
Posted Monday, July 19, 2010 12:08 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, September 09, 2013 7:57 AM
Points: 267, Visits: 93
Nice question!!
Post #954608
Posted Monday, July 19, 2010 12:31 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Wednesday, March 12, 2014 3:59 AM
Points: 1,938, Visits: 1,149
Nice question.

I did not notice the variable length.that's why i got wrong.I had answered 1.


Malleswarareddy
I.T.Analyst
MCITP(70-451)
Post #954615
Posted Monday, July 19, 2010 1:06 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, April 11, 2014 1:51 PM
Points: 2,628, Visits: 760
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' textfield
union select 'blue' textfield
union select 'green' textfield
union select 'yellow' textfield
union select 'orange' textfield
union select 'purple' textfield)x
where convert(varchar(1),textfield) like '%red%'

ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_6tsql/html/282cd982-f4fb-4b22-b2df-9e8478f13f6a.htm
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.

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).


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.


Jamie
Post #954620
Posted Monday, July 19, 2010 4:40 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:54 PM
Points: 5,787, Visits: 8,001
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!



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #954718
Posted Monday, July 19, 2010 4:41 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:54 PM
Points: 5,787, Visits: 8,001
SanjayAttray (7/18/2010)
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.

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.



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #954721
Posted Monday, July 19, 2010 7:04 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Wednesday, April 09, 2014 10:36 AM
Points: 896, Visits: 1,479
Nice question.
I had to browse my memory from long ago after I saw that VARCHAR declaration. I had forgotten that.

Thanks a lot.


Best regards,

Andre Guerreiro Neto

Database Analyst
http://www.softplan.com.br
MCITPx1/MCTSx2
Post #954788
Posted Monday, July 19, 2010 7:17 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Thursday, April 03, 2014 10:34 AM
Points: 3,352, Visits: 1,478
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
Post #954797
Posted Monday, July 19, 2010 7:23 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, May 14, 2013 1:42 PM
Points: 3,487, Visits: 1,581
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




Post #954800
Posted Monday, July 19, 2010 7:50 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Friday, February 04, 2011 7:20 AM
Points: 977, Visits: 1,499
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.


Tom Garth
Vertical Solutions

"There are three kinds of men. The one that learns by reading. The few who learn by observation. The rest of them have to pee on the electric fence for themselves." -- Will Rogers
Post #954818
Posted Monday, July 19, 2010 8:20 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 8:19 AM
Points: 2,288, Visits: 2,543
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:

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.

http://msdn.microsoft.com/en-us/library/ms176089.aspx

I 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


-------------------
"Operator! Give me the number for 911!" - Homer Simpson

"A SQL query walks into a bar and sees two tables. He walks up to them and says 'Can I join you?'"
Ref.: http://tkyte.blogspot.com/2009/02/sql-joke.html
Post #954837
« Prev Topic | Next Topic »

Add to briefcase ««1234»»»

Permissions Expand / Collapse