Solved

SQL Server CAST Usage

Posted on 2010-09-24
6
598 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
6 Comments
 
LVL 1

Expert Comment

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

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now