Solved

SQL Server CAST Usage

Posted on 2010-09-24
6
636 Views
Last Modified: 2012-05-10
Hi Experts,
My sql is;
select  cast(MyTable.MyField1 as varchar(5)) from MyTable

My field is;
COLUMN_NAME      DATA_TYPE      TYPE_NAME
MyField1           5                       smallint

I wonder What is the difference between;
1) cast(MyTable.MyField1 as varchar(5))
2) cast(MyTable.MyField1 as varchar)

Thanks,
0
Comment
Question by:kosturdur
[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
6 Comments
 
LVL 1

Expert Comment

by:boogiefromzk
ID: 33751969
varchar(5) is varchar, limited with 5 characters length
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
ID: 33751970
http://msdn.microsoft.com/en-us/library/ms187928.aspxlength    Is an optional integer that specifies the length of the target data type. The default value is 30. so:2) cast(MyTable.MyField1 as varchar)would be the same as2) cast(MyTable.MyField1 as varchar(30) )
0
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 200 total points
ID: 33751974
varchar(5) will allow 5 character space and only varchar will decide space at run time, best way is to use varchar(5), static allocation.
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 9

Assisted Solution

by:auke_t
auke_t earned 150 total points
ID: 33752114
Hi,

a smallint can be anyting between -2^15 (-32,768) to 2^15-1 (32,767). So you'd better use varchar(6)! ;-)


SELECT cast(-32768 as varchar(5))

SELECT cast(-32768 as varchar(6))

Cheers!

Auke
0
 
LVL 5

Author Comment

by:kosturdur
ID: 33752269
3 helpfull comments so 3 answers made me increase the points
0
 
LVL 5

Author Closing Comment

by:kosturdur
ID: 33752277
Thanks all
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

635 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