Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Think Twice Before You Convert

By Aries Manlig,

T-SQL comes with quite effective functions that help convert from one data type to another. But these can be ineffective if one lacks the knowledge of the results of these functions.

I was adding a debugging mechanism to my sproc by capturing the values of all the parameters. All the values are, by default, converted to varchar data type. When I started evaluating the values of the parameters in my sproc, I noticed that one of them, which has a data type of varchar(999), has its length truncated to a size of 30 characters. Then in my code I saw the following conversion:

	Declare @newValue varchar(999)
	Declare @oldValue varchar(999)
		
	Set @newValue = Convert(varchar, @oldValue)

After troubleshooting my code above, I've later found out that it truncates the length of my initial varchar(999) type variable to a size of 30. Of course, I was not pleased.

To correct the problem, I had to specify the size of the new varchar data type, as in the following code:

	Declare @newValue varchar(999)
	Declare @oldValue varchar(999)
			
	Set @newValue = Convert(varchar(999), @oldValue)  
	-- MUST SPECIFY SIZE AS IN THE ORIGINAL DECLARATION

Furthermore, I did my research over the Internet, and I googled the following: default varchar size sql. It turns out that according to this site from MSDN, http://msdn2.microsoft.com/en-us/library/ms176089.aspx, if the size is not specified when converting to varchar using Cast or Convert function, the default is 30. These codes were run against SQL 2000 and SQL 2005, and the default on both version is the same.

This is a valuable experience for me. And, I hope other coders who are not aware of the case as I described here will also learn from it.

© 2005 Aries J. Manlig
Absolute Software Corp

Total article views: 10875 | Views in the last 30 days: 1
 
Related Articles
FORUM

Help with converting varchar to datetime

Convert varchar to datetime

FORUM

converting varchar to numeric

converting varchar to numeric

FORUM

convert varchar to numeric

convert varchar to numeric

FORUM

Convert varchar to numeric

Please guide in Convert varchar to numeric

FORUM

Converting Getdate() To Varchar

Converting Getdate() to Varchar (16) to include Milliseconds

Tags
advanced querying    
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones