Solved

SQL Server CAST Usage

Posted on 2010-09-24
6
629 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

734 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