0% found this document useful (0 votes)
4 views

Conversion Functions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Conversion Functions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL Server CONVERT() Function

Definition and Usage


The CONVERT() function is a general function
for converting data into a new data type.
The CONVERT() function can be used to display
date/time data in different formats.
Syntax
CONVERT(data_type(length),data_to_be_conve
rted,style)

Where data_type(length) specifies the target


data type (with an optional length),
data_to_be_converted contains the value to be
converted, and style specifies the output format
for the date/time.
The styles that can be used are:

Style Style Format


ID

100 or 0 mon dd yyyy hh:miAM (or PM)

101 mm/dd/yy
102 yy.mm.dd

103 dd/mm/yy

104 dd.mm.yy

105 dd-mm-yy

106 dd mon yy

107 Mon dd, yy

108 hh:mm:ss

109 or 9 mon dd yyyy hh:mi:ss:mmmAM (or


PM)

110 mm-dd-yy

111 yy/mm/dd

112 yymmdd

113 or dd mon yyyy hh:mm:ss:mmm(24h)


13

114 hh:mi:ss:mmm(24h)

120 or yyyy-mm-dd hh:mi:ss(24h)


20
121 or yyyy-mm-dd hh:mi:ss.mmm(24h)
21

126 yyyy-mm-ddThh:mm:ss.mmm(no
spaces)

130 dd mon yyyy hh:mi:ss:mmmAM

131 dd/mm/yy hh:mi:ss:mmmAM

CONVERT(VARCHAR(19),GETDATE())
CONVERT(VARCHAR(10),GETDATE(),110)
CONVERT(VARCHAR(11),GETDATE(),106)
CONVERT(VARCHAR(24),GETDATE(),113)

The result would look something like this:

Nov 04 2008 11:45 PM


11-04-2008
04 Nov 08
04 Nov 2008 11:45:34:243
Declare @date date
select @date = CONVERT(date,
CAST('20010101' AS CHAR(12)),
112)
select @date

You might also like