Thursday, May 1, 2014

Difference between cast and convert in MS SQL



Difference between cast and convert


Syntax of cast and convert is as follows,

(    (1)   Select cast(<varchar-value> as int) from Table
(    (2)   Select Convert(int, <varchar-value>) from Table

There are two types of conversion of data format from one format to another, they are implicit and explicit,
Implicit way of conversion includes the server automatically converts the data without requiring the user to input any external data or coding.
Explicit way of conversion is where server accepts user input to covert the data to his desired format.


use temp1
create table dbo.custr (cuid int, custaddress varchar(50))
go
insert into dbo.custr values (1, '560056')


select CAST(custaddress as int)as value from dbo.custr

select CONVERT(int,custaddress) as value from dbo.custr



In above example column custaddress is having datatype of varchar which is of length of 50, we extracted the data in the format of int by using cast and covnert functions.

No comments:

Post a Comment