Total Pageviews

CONVERT() IN SQL SERVER



What is CONVERT ()

It is a system defined function which help us to convert an expression of one data type to another.

Syntax:-

CONVERT ( <target data type [& length]> , expression [ , style ] )

here:-

<target data type [,length]>:- Data type in which we want to convert the data type of our expression.
Expression:- A valid expression.
[style ]:- With this clause, we can specify how the convert function translate the expression.

The best feature of CONVERT() is to convert the date in desired format, below are some of examples :- 

SELECT GETUTCDATE() --- '2013-03-02 10:21:08.123'

SELECT CONVERT(VARCHAR(20), GETUTCDATE())  --  'Mar  2 2013 10:21AM'

SELECT CONVERT(VARCHAR(20), GETUTCDATE(), 101)  --  '03/02/2013' -- mm/dd/yyyy

SELECT CONVERT(VARCHAR(20), GETUTCDATE(), 103)  --  '02/03/2013' -- dd/mm/yyyy

SELECT CONVERT(VARCHAR(20), GETUTCDATE(), 110)  --  '03-02-2013' -- mm-dd-yyyy

No comments:

Post a Comment