Solved

SQL Server CAST Usage

Posted on 2010-09-24
6
585 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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL 2014 get SPIDs of users 6 26
Contained Database Collations 6 20
while loop in html mail format 5 32
Stored procedure 23 9
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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…

757 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

21 Experts available now in Live!

Get 1:1 Help Now