SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Think Twice Before You Convert

By Aries Manlig, 2005/12/22

Total article views: 10734 | Views in the last 30 days: 1

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

By Aries Manlig, 2005/12/22

Total article views: 10734 | Views in the last 30 days: 1
Your response
 
 
Related tags

Advanced Querying    
T-SQL    
 
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com