• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

Convert to varchar without length

I just realized that a lot of queries that are in our project have the following in them:

convert(varchar, @PARAM)

What is the affect of not specifying a length for the varchar?

convert(varchar(10), @PARAM)

I've seen in someone's notes that it defaults to 30 and I've tested that in SQL Analyzer and it holds true:

select convert(varchar, '1234567890123456789012345678901234567890')

returns: 123456789012345678901234567890 (30 chars)

Is this a default in QA or does this hold true?
0
r_i_x
Asked:
r_i_x
2 Solutions
 
mmburuCommented:
This is taken from SQL Server Books Online:

When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified with the CAST function, the default length is 30.

0
 
r_i_xAuthor Commented:
It doesn't explicitly say anything about CONVERT though. Are they treated one and the same?
0
 
mcmonapCommented:
Hi r_i_x,

Try this:
DECLARE @varchar1 VARCHAR(30)
DECLARE @varchar2 VARCHAR

SET @varchar1 = '1234567890123456789012345678901234567890'
SET @varchar2 = '1234567890123456789012345678901234567890'

select convert(varchar, '1234567890123456789012345678901234567890')
select @varchar1
select @varchar2

Not declaring the length of a varchar defaults its length to one, except in a cast statement where is defaults to 30, see the BOL articles here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_ca-co_7tpu.asp
0
 
sudheeshthegreatCommented:
yes, CAST and COVERT are basically treated the same
0
 
sudheeshthegreatCommented:
typo: CONVERT
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now