Unlimited Web Space

Enjoy Unlimited web hosting

Aug 12, 2011

CAST AND CONVERT SQL/MSDN

CAST ( ColumnName as datatype)

CAST (EmpID as varchar(20))
This makes the Column EmpID as varchar from what ever datatype it was.

CONVERT (datatype, @ColumnName)

CONVERT (varchar(20), @EmpID)


We can also use in different ways like
SELECT CONVERT(nvarchar(20),HelloWorld)
This shows HelloWorld by storing it as nvarchar.

In General,
Using CAST:
CAST ( expression AS data_type )
Using CONVERT:
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

No comments:

Post a Comment