Wednesday 29 June 2016

Cast and Convert in sql server

Cast and Convert both functions are used for convert data from one type to another.

Cast

The Cast () function is used to convert a data type variable or data from one data type to another data type. The Cast () function provides a data type to a dynamic parameter (?) or a NULL value.
CAST is part of the ANSI-SQL specification; whereas, CONVERT is not.  In fact, CONVERT is SQL implementation specific.
Syntax


select CAST(getdate() as Date)
as [Date]


Convert () Function

When we convert expressions from one type to another, in many cases there will be a need within a stored procedure or other routine to convert data from a datetime type to a Varchar type. The Convert function is used for such things. The CONVERT () function can be used to display date/time data in various formats.
CONVERT differences lie in that it accepts an optional style parameter which is used for formatting.
Syntax


select CONVERT(varchar(15), Getdate(), 103)
as [Today]


 Style 
Style values for datetime or smalldatetime conversion to character data.
select CONVERT(VARCHAR(19),GETDATE())
select CONVERT(VARCHAR(10),GETDATE(),10)
select CONVERT(VARCHAR(10),GETDATE(),110)
select CONVERT(VARCHAR(11),GETDATE(),6)
select CONVERT(VARCHAR(11),GETDATE(),106)
select CONVERT(VARCHAR(24),GETDATE(),113)


  

1 comment:

  1. I have read your blog its very attractive and impressive. I like your blog MSBI online training

    ReplyDelete

If you have any doubt, please let me know.

Popular Posts