?
Solved

Convert to varchar without length

Posted on 2005-03-21
5
Medium Priority
?
442 Views
Last Modified: 2006-11-17
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
Comment
Question by:r_i_x
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Accepted Solution

by:
mmburu earned 800 total points
ID: 13595550
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
 
LVL 1

Author Comment

by:r_i_x
ID: 13595605
It doesn't explicitly say anything about CONVERT though. Are they treated one and the same?
0
 
LVL 15

Expert Comment

by:mcmonap
ID: 13595626
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
 
LVL 9

Assisted Solution

by:sudheeshthegreat
sudheeshthegreat earned 200 total points
ID: 13599484
yes, CAST and COVERT are basically treated the same
0
 
LVL 9

Expert Comment

by:sudheeshthegreat
ID: 13599488
typo: CONVERT
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question