SQL Server Functions That Helps to Convert Date and Time Values to and From String Literals and...
-
Upload
manuelmartin3 -
Category
Documents
-
view
162 -
download
0
Transcript of SQL Server Functions That Helps to Convert Date and Time Values to and From String Literals and...
10,654,106 members (47,959 online)
Sign in
home quick answers discussions features community
helpSearch for articles, questions, tips
Articles » Database » Database » SQL Server
Article
Browse Code
Bugs / Suggestions
Stats
Revisions (3)
Alternatives
Comments (31)
View this article's
Workspace
Fork this Workspace
Connect using Git
Share
About Article
CAST(), CONVERT(),PARSE(), TRY_PARSE(),TRY_CONVERT(),FORMAT(), SQL ServerFunctions, SQL ServerFunctions that helps to
convert date and timevalues to and from stringliterals and other date andtime formats.
Type Article
Licence CPOL
First Posted 5 May 2013
Views 176,426
Downloads 908
Bookmarked 92 times
SQL2000
SQL-server-2005 , +
Top News
Visual Studio "14" CTP
Get the Insider News free eachmorning.
Related Videos
Next
Rate this:
SQL Server Functions that helps to convert
date and time values to and from string literals
and other date and time formats.By Shemeer NS, 20 May 2013
Prize winner in Competition "Best Database article of May 2013"
Download Scripts.zip - 4.3 KB
Table Of Contents
Introduction
Which are those functions?
More Info
CAST
Definition
Samples
CONVERT
Definition
Converting DATETIME to VARCHAR
Converting VARCHAR to DATETIME
Convert Styles
Samples
FORMAT
Definition
Standard Date and Time Format Strings
Custom Date and Time Format Strings
Samples
PARSE
Definition
4.95 (47 votes)
articles
Related Articles
SAPrefs - Netscape-likePreferences Dialog
XNA Snooker Club
Baboon Framework : ARevolutionary WPF Frameworkthat will Save You Time andEffort
WPF: A* search
DNS.NET Resolver (C#)
Windows 7 / VS2010 demo app
Plotting Circular RelationshipGraphs with Silverlight
Window Tabs (WndTabs) Add-In for DevStudio
WPF: If Carlsberg did MVVMFrameworks: Part 3 of n
WPF Localization Using RESXFiles
Multi-line InputBox control -MC++
Namespace extensions - theundocumented Windows Shell
WTL for MFC Programmers,Part IX - GDI Classes, CommonDialogs, and Utility Classes
OLE DB - First steps
Open a command promptanywhere.
Creating alternate GUI usingVector Art
DSLs: A Deep(ish) look
Remoting ManagementConsole
Nullsoft Winamp Plug-in WithBitmapped UI, Docking andRestrictive Resizing
Introduction to D3DImage
The Complete Idiot's Guide toWriting Shell Extensions - Part VI
Related Research
Insider Secrets on API SecurityFrom Experts at Securosis
[Webinar]
Ten Tips of Web App Testing
Samples
TRY_PARSE
Definition
Samples
TRY_CONVERT
Definition
Samples
References
Summary
Introduction
When we work with any programming languages we often required to perform data type casting
and converting. Conversion can be from one of the following options
one data type to another,
from one format to another format,
from one culture to another culture.
When talking about date and time, different languages uses different standards (format) and the
developer needs to take care about the date and or time format as per the geographic locations
(or languages). This article will go through all the SQL Server Functions that helps to convert date
and time values to and from string literals and other date and time formats. The function names
are given below,
Function Name 2000 2005 2008 2008 R2 2012
CAST() Y Y Y Y Y
CONVERT() Y Y Y Y Y
FORMAT() N N N N Y
PARSE() N N N N Y
TRY_PARSE() N N N N Y
TRY_CONVERT() N N N N Y
All the above functions except FORMAT() are Conversion Functions of Transact-SQL whereas
FORMAT() is a Transact-SQL String Function. We will go through all the above functions with
detailed samples. You can find all the SQL queries used here as a .zip attachment with this
article.
I hope you will enjoy this article. I have put my time and efforts on all of my articles, Please don't
forget to mark your votes, suggestions and feedback to improve the quality of this and upcoming
articles.
Note: All the above mentioned functions will be explained only from date and time perspective.
CAST()
Explicitly converts an expression of one data type to another.CAST and CONVERT provide similar
functionality.
Syntax :-
Collapse | Copy Code
CAST ( expression AS data_type [ (length)])
Here 'expression' can be any valid expression, 'data_type' is the target data type and 'length' is an
optional integer that specifies the length of the target data type. The default value is 30.
Custom API Management forthe Enterprise: Learn how to
build a successful API strategy[Webinar]
Enterprise Imaging on the Web:A How To Guide for Developers
Collapse | Copy Code
SELECT CAST('SQL Server Functions that helps to convert date and time values to and from string literals and other date and time formats' AS VARCHAR) AS TITLE
If you see the above sql query , I have not specified the length. so the length will be considered as
30 the default length for CAST , CONVERT.
Collapse | Copy Code
TITLE------------------------------SQL Server Functions that help
The output got truncated...The above SQL statement was just to demonstrate the use of length.
Let's see how we can cast a string date or string time to datetime data type.
Collapse | Copy Code
SELECT CAST('20130416' AS datetime) AS 'DateValue'SELECT CAST('2013-04-16' AS datetime) AS 'DateValue'SELECT CAST('11:20:25' AS datetime) AS 'TimeValue'
The above query will demonstrate the casting of string to a datetime data type. The output
generated are given below,
Collapse | Copy Code
DateValue-----------------------2013-04-16 00:00:00.000 DateValue-----------------------2013-04-16 00:00:00.000 TimeValue-----------------------1900-01-01 11:20:25.000
CONVERT()
Explicitly converts an expression of one data type to another.CASTandCONVERTprovide similar
functionality.
Syntax :-
Collapse | Copy Code
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
Here 'expression' can be any validexpression, 'data_type' is the target data type and 'length' is an
optional integer that specifies the length of the target data type. The default value is 30. Style is
an integer expression that specifies how the CONVERT function is to translate expression. If style
is NULL then NULL is returned. The range is determined by data_type.
Now we will explore the different styles of date and time formatting with CONVERT.
Converting DATETIME to VARCHAR
The Transact-SQL (T-SQL) Convert function can be used to convert data between different types.
When converting a DATETIME value to a VARCHAR value a style code can be used for getting the
output in different format.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 0) -- May 4 2013 2:14PM
Converting VARCHAR to DATETIME
The style code is equally important when converting a VARCHAR to a DATETIME value. I'm using
the output from the previous sql code and different style codes, lets see how it works.
Collapse | Copy Code
SELECT CONVERT(DATETIME, 'May 4 2013 2:14PM', 0) -- 2013-05-04 14:14:00.000SELECT CONVERT(DATETIME, 'May 4 2013 2:14PM', 130) -- Conversion failed when converting date and/or time from character string.
Date and Time Styles
When working with datetime, style can be one of the values shown in the following table. The
example column shows conversion from datetime to varchar and vice versa.
Date Format Code Standard and Examples
mon dd yyyy hh:miAM
(or PM)
0 or
100
Default. Equivalent to not specifying a style code.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 0) -- May 4 2013 2:14PMSELECT CONVERT(VARCHAR, GETDATE(), 100) -- May 4 2013 2:14PMSELECT CONVERT(DATETIME, 'May 4 2013 2:14PM', 0) -- 2013-05-04 14:14:00.000SELECT CONVERT(DATETIME, 'May 4 2013 2:14PM', 100) -- 2013-05-04 14:14:00.000
mm/dd/yy 1
U.S.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 1) -- 05/04/13SELECT CONVERT(DATETIME, '05/04/13', 1) -- 2013-05-04 00:00:00.000
mm/dd/yyyy 101
U.S.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 101) -- 05/04/2013SELECT CONVERT(DATETIME, '05/04/2013', 101) -- 2013-05-04 00:00:00.000
yy.mm.dd 2
ANSI.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 2) -- 13.05.04SELECT CONVERT(DATETIME, '13.05.04', 2) -- 2013-05-04 00:00:00.000
yyyy.mm.dd 102
ANSI.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 102) -- 2013.05.04SELECT CONVERT(DATETIME, '2013.05.04', 102) -- 2013-05-04 00:00:00.000
dd/mm/yy 3
British/French.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 3) -- 04/05/13SELECT CONVERT(DATETIME, '04/05/13', 3) -- 2013-05-04 00:00:00.000
British/French.
Collapse | Copy Code
dd/mm/yyyy 103 SELECT CONVERT(VARCHAR, GETDATE(), 103) -- 04/05/2013SELECT CONVERT(DATETIME, '04/05/2013', 103) -- 2013-05-04 00:00:00.000
dd.mm.yy 4
German.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 4) -- 04.05.13SELECT CONVERT(DATETIME, '04.05.13', 4) -- 2013-05-04 00:00:00.000
dd.mm.yyyy 104
German.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 104) -- 04.05.2013SELECT CONVERT(DATETIME, '04.05.2013', 104) -- 2013-05-04 00:00:00.000
dd-mm-yy 5
Italian.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 5) -- 04-05-13SELECT CONVERT(DATETIME, '04-05-13', 5) -- 2013-05-04 00:00:00.000
dd-mm-yyyy 105
Italian.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 105) -- 04-05-2013SELECT CONVERT(DATETIME, '04-05-2013', 105) -- 2013-05-04 00:00:00.000
dd mon yy 6
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 6) -- 04 May 13SELECT CONVERT(DATETIME, '04 May 13', 6) -- 2013-05-04 00:00:00.000
dd mon yyyy 106
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 106) -- 04 May 2013SELECT CONVERT(DATETIME, '04 May 2013', 106) -- 2013-05-04 00:00:00.000
Mon dd, yy 7
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 7) -- May 04, 13SELECT CONVERT(DATETIME, 'May 04, 13', 7) -- 2013-05-04 00:00:00.000
Mon dd, yyyy 107
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 107) -- May 04, 2013SELECT CONVERT(DATETIME, 'May 04, 2013', 107) -- 2013-05-04 00:00:00.000
hh:mi:ss 8
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 8) -- 14:14:35SELECT CONVERT(DATETIME, '14:14:35', 8) -- 1900-01-01 14:14:35.000 sdfsdf
Collapse | Copy Code
hh:mi:ss 108 SELECT CONVERT(VARCHAR, GETDATE(), 108) -- 14:14:35SELECT CONVERT(DATETIME, '14:14:35', 108) -- 1900-01-01 14:14:35.000
mon dd yyyy
hh:mi:ss:mmmAM (or
PM)
9 or
109
Default + milliseconds.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 9) -- May 4 2013 2:14:35:020PMSELECT CONVERT(DATETIME, 'May 4 2013 2:14:35:020PM', 9) -- 2013-05-04 14:14:35.020SELECT CONVERT(VARCHAR, GETDATE(), 109) -- May 4 2013 2:14:35:020PMSELECT CONVERT(DATETIME, 'May 4 2013 2:14:35:020PM', 109) -- 2013-05-04 14:14:35.020
mm-dd-yy 10
USA.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 10) -- 05-04-13SELECT CONVERT(DATETIME, '05-04-13', 10) -- 2013-05-04 00:00:00.000
mm-dd-yyyy 110
USA.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 110) -- 05-04-2013SELECT CONVERT(DATETIME, '05-04-2013', 110) -- 2013-05-04 00:00:00.000
yy/mm/dd 11
JAPAN.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 11) -- 13/05/04SELECT CONVERT(DATETIME, '13/05/04', 11) -- 2013-05-04 00:00:00.000
yyyy/mm/dd 111
JAPAN.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 111) -- 2013/05/04SELECT CONVERT(DATETIME, '2013/05/04', 111) -- 2013-05-04 00:00:00.000
yymmdd 12
ISO.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 12) -- 130504SELECT CONVERT(DATETIME, '130504', 12) -- 2013-05-04 00:00:00.000
yyyymmdd 112
ISO.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 112) -- 20130504SELECT CONVERT(DATETIME, '20130504', 112) -- 2013-05-04 00:00:00.000
Europe default + milliseconds.
dd mon yyyy
hh:mi:ss:mmm(24h)
13 or
113
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 13) -- 04 May 2013 14:14:35:020SELECT CONVERT(DATETIME, '04 May 2013 14:14:35:020', 13) -- 2013-05-04 14:14:35.020SELECT CONVERT(VARCHAR, GETDATE(), 113) -- 04 May 2013 14:14:35:020SELECT CONVERT(DATETIME, '04 May 2013 14:14:35:020', 113) -- 2013-05-04 14:14:35.020
hh:mi:ss:mmm(24h) 14
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 14) -- 14:14:35:020SELECT CONVERT(DATETIME, '14:14:35:020', 14) -- 1900-01-01 14:14:35.020
hh:mi:ss:mmm(24h) 114
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 114) -- 14:14:35:020SELECT CONVERT(DATETIME, '14:14:35:020', 114) -- 1900-01-01 14:14:35.020
yyyy-mm-dd
hh:mi:ss(24h)
20 or
120
ODBC canonical.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 20) -- 2013-05-04 14:14:35SELECT CONVERT(DATETIME, '2013-05-04 14:14:35', 20) -- 2013-05-04 14:14:35.000SELECT CONVERT(VARCHAR, GETDATE(), 120) -- 2013-05-04 14:14:35SELECT CONVERT(DATETIME, '2013-05-04 14:14:35', 120) -- 2013-05-04 14:14:35.000
yyyy-mm-dd
hh:mi:ss.mmm(24h)
21 or
121
ODBC canonical (with milliseconds).
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 21) -- 2013-05-04 14:14:35.073SELECT CONVERT(DATETIME, '2013-05-04 14:14:35.073', 21) -- 2013-05-04 14:14:35.073SELECT CONVERT(VARCHAR, GETDATE(), 121) -- 2013-05-04 14:14:35.073SELECT CONVERT(DATETIME, '2013-05-04 14:14:35.073', 121) -- 2013-05-04 14:14:35.073
yyyy-mm-
ddThh:mi:ss.mmm (no
spaces)
126
ISO8601.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 126) -- 2013-05-04T14:14:35.073SELECT CONVERT(DATETIME, '2013-05-04T14:14:35.073', 126) -- 2013-05-04 14:14:35.073
yyyy-mm-
ddThh:mi:ss.mmmZ (no
spaces)
127
ISO8601 with time zone Z. Designed for XML use.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 127) -- 2013-05-04T14:14:35.073SELECT CONVERT(DATETIME, '2013-05-04T14:14:35.073', 127) -- 2013-05-04 14:14:35.073
dd mon yyyy
hh:mi:ss:mmmAM130
Hijri.
Collapse | Copy Code
SELECT CONVERT(NVARCHAR, GETDATE(), 130) -- 24 جمادى الثانیة 1434 2:14:35SELECT CONVERT(DATETIME, '24 1434 جمادى الثانیة
14:14:35.073 04-05-2013 -- (130 ,'2:14:35
dd/mm/yy
hh:mi:ss:mmmAM131
Hijri.
Collapse | Copy Code
SELECT CONVERT(VARCHAR, GETDATE(), 131) -- 24/06/1434 2:14:35:073PMSELECT CONVERT(DATETIME, '24/06/1434 2:14:35:073PM', 131) -- 2013-05-04 14:14:35.073
The Date Format strings are just for representational purpose and cannot be used directly in
FORMAT() function's format parameter. The default values (style 0 or 100, 9 or 109, 13 or 113, 20
or 120, and 21 or 121) always return the century (yyyy). Hijri is a calendar system with several
variations. SQL Server uses the Kuwaiti algorithm.
FORMAT()
Returns a value formatted with the specified format and optional culture. Use the FORMAT
function for locale-aware formatting of date/time and number values as strings. FORMAT returns
NULL for errors other than a culture that is not valid. For example, NULL is returned if the value
specified in format is not valid. FORMAT relies on the presence of .the .NET Framework Common
Language Runtime (CLR). Click here to read more from msdn.
Syntax :-
Collapse | Copy Code
FORMAT ( value, format [, culture ] )
Where value is an expression of a supported data type to format. The format argument must
contain a valid .NET Framework format string, either as a standard format string or as a pattern of
custom characters for dates and numeric values. Composite formatting is not supported. format
argument is in an nvarchar format pattern.
From below tables we can learn standard and custom date and time formatting options with
FORMAT funtion.
Standard Date and Time Format Strings
A standard date and time format string uses a single format specifier to define the text
representation of a date and time value. Below given table shows all standard date and time
format strings, if you use any single character as format string then it will consider it as a single
format specifier (standard), if it's an invalid single format specifier then NULL will be retuned.
In a formatting operation, a standard format string is simply an alias for a custom format string.
The advantage of using an alias to refer to a custom format string is that, although the alias
remains invariant, the custom format string itself can vary. This is important because the string
representations of date and time values typically vary by culture. For example, the "d" standard
format string indicates that a date and time value is to be displayed using a short date pattern.
For the invariant culture, this pattern is "MM/dd/yyyy". For the fr-FR culture, it is "dd/MM/yyyy".
For the ja-JP culture, it is "yyyy/MM/dd".
Single
Format
Specifier
Description and Examples
'd'
Short date pattern.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/28/2013 4:54:08 PM'
SELECT FORMAT(@d, 'd') --4/28/2013SELECT FORMAT(@d, 'd', 'en-US') --4/28/2013SELECT FORMAT(@d, 'd', 'fr-FR') --28/04/2013SELECT FORMAT(@d, 'd', 'ja-JP'); --2013/04/28
'D'
Long date pattern.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/28/2013 4:54:08 PM' SELECT FORMAT(@d, 'D') --Sunday, April 28, 2013SELECT FORMAT(@d, 'D', 'sv-SE') --den 28 april 2013SELECT FORMAT(@d, 'D', 'fr-FR') --dimanche 28 avril 2013SELECT FORMAT(@d, 'D', 'ja-JP'); --2013年4月28日
'f'
Full date/time pattern (short time).
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/28/2013 4:54:08 PM' SELECT FORMAT(@d, 'f') --Sunday, April 28, 2013 4:54 PMSELECT FORMAT(@d, 'f', 'de-DE') --Sonntag, 28. April 2013 16:54SELECT FORMAT(@d, 'f', 'fr-FR') --dimanche 28 avril 2013 16:54SELECT FORMAT(@d, 'f', 'sv-SE'); --den 28 april 2013 16:54
'g'
Full date/time pattern (long time).
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/28/2013 4:54:08 PM' SELECT FORMAT(@d, 'g') --4/28/2013 4:54 PMSELECT FORMAT(@d, 'g', 'de-DE') --28.04.2013 16:54SELECT FORMAT(@d, 'g', 'fr-FR') --28/04/2013 16:54SELECT FORMAT(@d, 'g', 'sv-SE'); --2013-04-28 16:54
'G'
General date/time pattern (short time).
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/28/2013 4:54:08 PM' SELECT FORMAT(@d, 'G') --4/28/2013 4:54:08 PMSELECT FORMAT(@d, 'G', 'hr-HR') --28.4.2013. 16:54:08SELECT FORMAT(@d, 'G', 'id-ID') --28/04/2013 16:54:08SELECT FORMAT(@d, 'G', 'el-GR'); --28/4/2013 4:54:08 μμ
'M', 'm'
Month/day pattern.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/28/2013 4:54:08 PM' SELECT FORMAT(@d, 'M') --April 28SELECT FORMAT(@d, 'M', 'hr-HR') --28. travnjaSELECT FORMAT(@d, 'M', 'id-ID') --28 AprilSELECT FORMAT(@d, 'M', 'el-GR'); --28 ΑπριλίουSELECT FORMAT(@d, 'm') --April 28SELECT FORMAT(@d, 'm', 'hr-HR') --28. travnjaSELECT FORMAT(@d, 'm', 'id-ID') --28 AprilSELECT FORMAT(@d, 'm', 'el-GR'); --28 Απριλίου
Round-trip date/time pattern. The pattern for this specifier reflects a defined
standard (ISO 8601). Therefore, it is always the same regardless of the culture used
or the format provider supplied.
'O', 'o'
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/28/2013 4:54:08 PM' SELECT FORMAT(@d, 'O') --2013-04-28T16:54:08.0000000SELECT FORMAT(@d, 'O', 'hr-HR') --2013-04-28T16:54:08.0000000SELECT FORMAT(@d, 'O', 'id-ID') --2013-04-28T16:54:08.0000000SELECT FORMAT(@d, 'O', 'el-GR'); --2013-04-28T16:54:08.0000000SELECT FORMAT(@d, 'o') --2013-04-28T16:54:08.0000000SELECT FORMAT(@d, 'o', 'hr-HR') --2013-04-28T16:54:08.0000000SELECT FORMAT(@d, 'o', 'id-ID') --2013-04-28T16:54:08.0000000SELECT FORMAT(@d, 'o', 'el-GR'); --2013-04-28T16:54:08.0000000
'R', 'r'
RFC1123 pattern.The output will be same regardless of the culture used or the
format provider supplied.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/28/2013 4:54:08 PM' SELECT FORMAT(@d, 'R') --Sun, 28 Apr 2013 16:54:08 GMTSELECT FORMAT(@d, 'R', 'hr-HR') --Sun, 28 Apr 2013 16:54:08 GMTSELECT FORMAT(@d, 'R', 'id-ID') --Sun, 28 Apr 2013 16:54:08 GMTSELECT FORMAT(@d, 'R', 'el-GR'); --Sun, 28 Apr 2013 16:54:08 GMTSELECT FORMAT(@d, 'r') --Sun, 28 Apr 2013 16:54:08 GMTSELECT FORMAT(@d, 'r', 'hr-HR') --Sun, 28 Apr 2013 16:54:08 GMTSELECT FORMAT(@d, 'r', 'id-ID') --Sun, 28 Apr 2013 16:54:08 GMTSELECT FORMAT(@d, 'r', 'el-GR'); --Sun, 28 Apr 2013 16:54:08 GMT
's'
Sortable date/time pattern. The pattern reflects a defined standard (ISO 8601), and
the property is read-only. Therefore, it is always the same, regardless of the culture
used or the format provider supplied.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/28/2013 4:54:08 PM' SELECT FORMAT(@d, 's') --2013-04-28T16:54:08SELECT FORMAT(@d, 's', 'hr-HR') --2013-04-28T16:54:08SELECT FORMAT(@d, 's', 'id-ID') --2013-04-28T16:54:08SELECT FORMAT(@d, 's', 'el-GR'); --2013-04-28T16:54:08
't'
Short time pattern.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/28/2013 4:54:08 PM' SELECT FORMAT(@d, 't') --4:54 PMSELECT FORMAT(@d, 't', 'hr-HR') --16:54SELECT FORMAT(@d, 't', 'ar-EG') --04:54 مSELECT FORMAT(@d, 't', 'el-GR'); --4:54 μμ
'T'
Long time pattern.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/28/2013 4:54:08 PM' SELECT FORMAT(@d, 'T') --4:54:08 PMSELECT FORMAT(@d, 'T', 'hr-HR') --16:54:08SELECT FORMAT(@d, 'T', 'ar-EG') --04:54:08 مSELECT FORMAT(@d, 'T', 'el-GR'); --4:54:08 μμ
'u'
Universal sortable date/time pattern. The output will be same regardless of the
culture used or the format provider supplied.
Collapse | Copy Code
DECLARE @d DATETIME
SET @d='4/28/2013 4:54:08 PM' SELECT FORMAT(@d, 'u') --2013-04-28 16:54:08ZSELECT FORMAT(@d, 'u', 'hr-HR') --2013-04-28 16:54:08ZSELECT FORMAT(@d, 'u', 'ar-EG') --2013-04-28 16:54:08ZSELECT FORMAT(@d, 'u', 'el-GR'); --2013-04-28 16:54:08Z
'U'
Universal full date/time pattern.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/28/2013 4:54:08 PM' SELECT FORMAT(@d, 'U') --Sunday, April 28, 2013 11:24:08 AMSELECT FORMAT(@d, 'U', 'hr-HR') --28. travnja 2013. 11:24:08SELECT FORMAT(@d, 'U', 'SV-se') --den 28 april 2013 11:24:08SELECT FORMAT(@d, 'U', 'el-GR'); --Κυριακή, 28 Απριλίου 2013 11:24:08 πμ
'Y', 'y'
Year month pattern.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/28/2013 4:54:08 PM' SELECT FORMAT(@d, 'Y') --April, 2013SELECT FORMAT(@d, 'Y', 'hr-HR') --travanj, 2013SELECT FORMAT(@d, 'Y', 'id-ID') --April, 2013SELECT FORMAT(@d, 'Y', 'el-GR'); --Απρίλιος 2013SELECT FORMAT(@d, 'y') --April, 2013SELECT FORMAT(@d, 'y', 'hr-HR') --travanj, 2013SELECT FORMAT(@d, 'y', 'id-ID') --April, 2013SELECT FORMAT(@d, 'y', 'el-GR'); --Απρίλιος 2013
Any
other
single
character
Unknown specifier. Returns NULL.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/28/2013 4:54:08 PM' SELECT FORMAT(@d, 'Q') --NULL
Remarks: The standard format string (Single Format Specifier) serves as a convenient
abbreviation for a longer custom format string that is invariant. Four standard
format strings fall into this category: "O" (or "o"), "R" (or "r"), "s", and "u". These
strings correspond to custom format strings defined by the invariant culture. They
produce string representations of date and time values that are intended to be
identical across cultures.
Single Format Specifier Equivalent Custom Format String
'O', 'o 'yyyy-MM-ddTHH:mm:ss.fffffff'
'R', 'r' 'ddd, dd MMM yyyy HH:mm:ss "GMT"'
's' 'yyyy-MM-ddTHH:mm:ss'
'u' 'yyyy-MM-dd HH:mm:ss"Z"'
Note : A single format specifier used as standard date and time format string to
define the text representation of a date and time value. Any date and time format
string that contains more than one character, including white space, is interpreted as
a custom date and time format string;
All single format specifier is easy to use if incase we want more customized format then we can
go for cutom date and time format string.
Custom Date and Time Format Strings
A custom format string consists of one or more custom date and time format specifiers. Any
string that is not a standard date and time format string is interpreted as a custom date and time
format string.
Custom
Format
Specifier
Description and Examples
Note : To use any of the custom date and time format specifiers as the only specifier
in a format string (that is, to use the "d", "f", "F", "g", "h", "H", "K", "m", "M", "s", "t",
"y", "z", ":", or "/" custom format specifier by itself), include a space before or after the
specifier, or include a percent ("%") format specifier before the single custom date
and time specifier.
--Day Format
'd'
The day of the month, from 1 through 31.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/9/2013 4:54:08 PM' SELECT FORMAT(@d, 'd ')--9 SELECT FORMAT(@d, 'd-M-yyyy')--9-4-2013SELECT FORMAT(@d, 'd MMMM')--9 AprilSELECT FORMAT(@d, 'd MMMM', 'fr-FR')--9 avril
'dd'
The day of the month, from 01 through 31.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/9/2013 4:54:08 PM' SELECT FORMAT(@d, 'dd')--09 SELECT FORMAT(@d, 'dd-M-yyyy')--09-4-2013SELECT FORMAT(@d, 'dd MMMM')--09 AprilSELECT FORMAT(@d, 'dd MMMM', 'fr-FR')--09 avril
'ddd'
The abbreviated name of the day of the week.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/9/2013 4:54:08 PM' SELECT FORMAT(@d, 'ddd d')--Tue 9 SELECT FORMAT(@d, 'ddd dd-M-yyyy')--Tue 09-4-2013SELECT FORMAT(@d, 'ddd dd MMMM')--Tue 09 AprilSELECT FORMAT(@d, 'ddd dd MMMM', 'fr-FR')--mar. 09 avril
'dddd'
The full name of the day of the week.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/9/2013 4:54:08 PM' SELECT FORMAT(@d, 'dddd d')--Tuesday 9 SELECT FORMAT(@d, 'dddd dd-M-yyyy')--Tuesday 09-4-2013SELECT FORMAT(@d, 'dddd dd MMMM')--Tuesday 09 AprilSELECT FORMAT(@d, 'dddd dd MMMM', 'fr-FR')--mardi 09 avril
--Month Format
The month, from 1 through 12.
'M' Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/9/2013 4:54:08 PM' SELECT FORMAT(@d, 'M ')--4 SELECT FORMAT(@d, 'dd-M-yyyy', 'fr-FR')--09-4-2013
'MM'
The month, from 01 through 12.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/9/2013 4:54:08 PM' SELECT FORMAT(@d, 'MM')--04 SELECT FORMAT(@d, 'dd-MM-yyyy', 'fr-FR')--09-04-2013
'MMM'
The abbreviated name of the month.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/9/2013 4:54:08 PM' SELECT FORMAT(@d, 'MMM')--Apr SELECT FORMAT(@d, 'MMM', 'ml-IN')--ഏ�റി�SELECT FORMAT(@d, 'MMM', 'fr-FR')--avr.
'MMMM'
The full name of the month.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/9/2013 4:54:08 PM' SELECT FORMAT(@d, 'MMMM')--April SELECT FORMAT(@d, 'MMMM', 'ml-IN')--ഏ�റി�SELECT FORMAT(@d, 'MMMM', 'fr-FR')--avril
--Year Format
'y'
The year, from 0 to 99.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/9/2001 4:54:08 PM' SELECT FORMAT(@d, 'y ')--1 SELECT FORMAT(@d, 'd-M-y')--9-4-1SELECT FORMAT(@d, 'd MMMM y')--9 April 1SELECT FORMAT(@d, 'd MMMM y', 'fr-FR')--9 avril 1
'yy'
The year, from 00 to 99.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/9/2001 4:54:08 PM' SELECT FORMAT(@d, 'yy')--01SELECT FORMAT(@d, 'd-M-yy')--9-4-01SELECT FORMAT(@d, 'd MMMM yy')--9 April 01SELECT FORMAT(@d, 'd MMMM yy', 'fr-FR')--9 avril 01
'yyy'
The year, with a minimum of three digits.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/9/2001 4:54:08 PM' SELECT FORMAT(@d, 'yyy')--2001
SELECT FORMAT(@d, 'd-M-yyy')--9-4-2001SELECT FORMAT(@d, 'd MMMM yyy')--9 April 2001SELECT FORMAT(@d, 'd MMMM yyy', 'fr-FR')--9 avril 2001
'yyyy'
The year as a four-digit number.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/9/2001 4:54:08 PM' SELECT FORMAT(@d, 'yyyy')--2001SELECT FORMAT(@d, 'd-M-yyyy')--9-4-2001SELECT FORMAT(@d, 'd MMMM yyyy')--9 April 2001SELECT FORMAT(@d, 'd MMMM yyyy', 'fr-FR')--9 avril 2001
'yyyyy'
The year as a five-digit number.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/9/2001 4:54:08 PM' SELECT FORMAT(@d, 'yyyyy')--02001SELECT FORMAT(@d, 'd-M-yyyyy')--9-4-02001SELECT FORMAT(@d, 'd MMMM yyyyy')--9 April 02001SELECT FORMAT(@d, 'd MMMM yyyyy', 'fr-FR')--9 avril 02001
--Era Format
'g', 'gg'
The period or era.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='4/9/2001 4:54:08 PM' SELECT FORMAT(@d, 'MM/dd/yyyy g')--04/09/2001 A.D.SELECT FORMAT(@d, 'MM/dd/yyyy g', 'fr-FR')--04/09/2001 ap. J.-C.SELECT FORMAT(@d, 'MM/dd/yyyy gg')--04/09/2001 A.D.SELECT FORMAT(@d, 'MM/dd/yyyy gg', 'fr-FR')--04/09/2001 ap. J.-C.
--Hour (Time Format)
'h'
The hour, using a 12-hour clock from 1 to 12.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='5/9/2001 4:54:08 PM' SELECT FORMAT(@d, 'h ')--4SELECT FORMAT(@d, 'h:m:s')--4:54:8SELECT FORMAT(@d, 'h:m:s', 'fr-FR')--4:54:8
'hh'
The hour, using a 12-hour clock from 01 to 12.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='5/9/2001 4:54:08 PM' SELECT FORMAT(@d, 'hh')--04SELECT FORMAT(@d, 'hh:m:s')--04:54:8SELECT FORMAT(@d, 'hh:m:s', 'fr-FR')--04:54:8
'H'
The hour, using a 24-hour clock from 0 to 23.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='5/9/2001 6:54:08 AM' SELECT FORMAT(@d, 'H ')--6
SELECT FORMAT(@d, 'H:m:s')--6:54:8SELECT FORMAT(@d, 'H:m:s', 'fr-FR')--6:54:8
'HH'
The hour, using a 24-hour clock from 00 to 23.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='5/9/2001 6:54:08 AM' SELECT FORMAT(@d, 'HH')--06SELECT FORMAT(@d, 'HH:m:s')--06:54:8SELECT FORMAT(@d, 'HH:m:s', 'fr-FR')--06:54:8
--Min (Time Format)
'm'
The minute, from 0 through 59.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='5/9/2001 6:09:08 AM' SELECT FORMAT(@d, 'm ')--9SELECT FORMAT(@d, 'mm')--09
'mm'
The minute, from 00 through 59.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='5/9/2001 6:09:08 AM' SELECT FORMAT(@d, 'm ')--9SELECT FORMAT(@d, 'mm')--09
--Sec (Time Format)
's'
The second, from 0 through 59.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='5/9/2001 6:09:08 AM' SELECT FORMAT(@d, 's ')--8SELECT FORMAT(@d, 'ss')--08
'ss'
The second, from 00 through 59.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='5/9/2001 6:09:08 AM' SELECT FORMAT(@d, 's ')--8SELECT FORMAT(@d, 'ss')--08
--Sec Fraction (Time Format)
'f'
The tenths of a second in a date and time value.
Collapse | Copy Code
DECLARE @d DATETIME2SET @d='5/9/2001 6:09:59.123456789 AM' SELECT FORMAT(@d, 'hh:mm:ss:f')--06:09:59:1
The hundredths of a second in a date and time value.
'ff' Collapse | Copy Code
DECLARE @d DATETIME2SET @d='5/9/2001 6:09:59.123456789 AM' SELECT FORMAT(@d, 'hh:mm:ss:ff')--06:09:59:12
'fff'
The milliseconds in a date and time value.
Collapse | Copy Code
DECLARE @d DATETIME2SET @d='5/9/2001 6:09:59.123456789 AM' SELECT FORMAT(@d, 'hh:mm:ss:fff')--06:09:59:123
'ffff'
The ten thousandths of a second in a date and time value.
Collapse | Copy Code
DECLARE @d DATETIME2SET @d='5/9/2001 6:09:59.123456789 AM' SELECT FORMAT(@d, 'hh:mm:ss:ffff')--06:09:59:1234
'fffff
The hundred thousandths of a second in a date and time value.
Collapse | Copy Code
DECLARE @d DATETIME2SET @d='5/9/2001 6:09:59.123456789 AM' SELECT FORMAT(@d, 'hh:mm:ss:fffff')--06:09:59:12345
'ffffff'
The millionths of a second in a date and time value.
Collapse | Copy Code
DECLARE @d DATETIME2SET @d='5/9/2001 6:09:59.123456789 AM' SELECT FORMAT(@d, 'hh:mm:ss:ffffff')--06:09:59:123456
'fffffff'
The ten millionths of a second in a date and time value.
Collapse | Copy Code
DECLARE @d DATETIME2SET @d='5/9/2001 6:09:59.123456789 AM' SELECT FORMAT(@d, 'hh:mm:ss:fffffff')--06:09:59:1234568
'F'
If non-zero, the tenths of a second in a date and time value.
Collapse | Copy Code
DECLARE @d DATETIME2SET @d='5/9/2001 6:09:59.012345009 AM' SELECT FORMAT(@d, 'hh:mm:ss f')--06:09:59 0SELECT FORMAT(@d, 'hh:mm:ss F')--06:09:59
'FF'
If non-zero, the hundredths of a second in a date and time value.
Collapse | Copy Code
DECLARE @d DATETIME2SET @d='5/9/2001 6:09:59.012345009 AM' SELECT FORMAT(@d, 'hh:mm:ss ff')--06:09:59 01SELECT FORMAT(@d, 'hh:mm:ss FF')--06:09:59 01
'FFF
If non-zero, the milliseconds in a date and time value.
Collapse | Copy Code
DECLARE @d DATETIME2SET @d='5/9/2001 6:09:59.012345009 AM' SELECT FORMAT(@d, 'hh:mm:ss fff')--06:09:59 012SELECT FORMAT(@d, 'hh:mm:ss FFF')--06:09:59 012
'FFFF'
If non-zero, the ten thousandths of a second in a date and time value.
Collapse | Copy Code
DECLARE @d DATETIME2SET @d='5/9/2001 6:09:59.012345009 AM' SELECT FORMAT(@d, 'hh:mm:ss ffff')--06:09:59 0123SELECT FORMAT(@d, 'hh:mm:ss FFFF')--06:09:59 0123
'FFFFF'
If non-zero, the hundred thousandths of a second in a date and time value.
Collapse | Copy Code
DECLARE @d DATETIME2SET @d='5/9/2001 6:09:59.012345009 AM' SELECT FORMAT(@d, 'hh:mm:ss fffff')--06:09:59 01234SELECT FORMAT(@d, 'hh:mm:ss FFFFF')--06:09:59 01234
'FFFFFF'
If non-zero, the millionths of a second in a date and time value.
Collapse | Copy Code
DECLARE @d DATETIME2SET @d='5/9/2001 6:09:59.012345009 AM' SELECT FORMAT(@d, 'hh:mm:ss ffffff')--06:09:59 012345SELECT FORMAT(@d, 'hh:mm:ss FFFFFF')--06:09:59 012345
'FFFFFFF'
If non-zero, the ten millionths of a second in a date and time value.
Collapse | Copy Code
DECLARE @d DATETIME2SET @d='5/9/2001 6:09:59.012345009 AM' SELECT FORMAT(@d, 'hh:mm:ss fffffff')--06:09:59 0123450SELECT FORMAT(@d, 'hh:mm:ss FFFFFFF')--06:09:59 012345
--Time Zone (Time Format)
'K'
Time zone information.
Collapse | Copy Code
DECLARE @d DATETIMEOFFSETSET @d='6/15/2009 1:45:30 AM -07:00' SELECT FORMAT(@d, 'K ')-- -07:00
--AM/PM designator (Time Format)
't'
The first character of the AM/PM designator.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='5/9/2001 6:09:59 AM' SELECT FORMAT(@d, 'hh:mm:ss t')--06:09:59 A
'tt'
The AM/PM designator.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='5/9/2001 6:09:59 AM' SELECT FORMAT(@d, 'hh:mm:ss tt')--06:09:59 AM
--Offset, UTC (Time Format)
'z'
Hours offset from UTC, with no leading zeros.
Collapse | Copy Code
DECLARE @d DATETIMEOFFSETSET @d='6/15/2009 1:45:30 AM +05:30' SELECT FORMAT(@d, 'z ') -- +5
'zz'
Hours offset from UTC, with a leading zero for a single-digit value.
Collapse | Copy Code
DECLARE @d DATETIMEOFFSETSET @d='6/15/2009 1:45:30 AM +5:30' SELECT FORMAT(@d, 'zz') -- +05
'zzz'
Hours and minutes offset from UTC.
Collapse | Copy Code
DECLARE @d DATETIMEOFFSETSET @d='6/15/2009 1:45:30 AM +5:30' SELECT FORMAT(@d, 'zzz') -- +05:30
--Date, Time Separator (Time Format)
':'
The time separator.
Collapse | Copy Code
SELECT FORMAT(GETDATE(), 'hh:mm:ss tt') -- 10:52:15 PM
'/'
The date separator.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='5/9/2001 6:09:59 AM' SELECT FORMAT ( @d, 'yyyy/MM/dd hh:mm:ss tt', 'en-US' ); --2001/05/09 06:09:59 AM
--String
"string"
Literal string delimiter.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='5/9/2001 6:09:59 AM' SELECT FORMAT ( @d, '"Today is "yyyy/MM/dd', 'en-US' ); --Today is 2001/05/09SELECT FORMAT ( @d, '"Mail sent on: "yyyy/MM/dd' ); --Mail sent on: 2001/05/09
--Escape Character
\
The escape character.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='5/9/2001 6:09:59 AM' SELECT FORMAT ( @d, 'h hour', 'en-US' ); --6 6our ,not as expectdSELECT FORMAT ( @d, 'h \hour' ); --6 hourSELECT FORMAT ( @d, 'MM \m' ); --05 mSELECT FORMAT ( @d, 'MM m' ); --05 9, not as expected
--Single Custom Format Specifier
%
Defines the following character as a custom format specifier. To use any of the
custom date and time format specifiers as the only specifier in a format string (that
is, to use the "d", "f", "F", "g", "h", "H", "K", "m", "M", "s", "t", "y", "z", ":", or "/" custom
format specifier by itself), include a space before or after the specifier, or include a
percent ("%") format specifier before the single custom date and time specifier.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='5/9/2001 6:09:59 AM' SELECT FORMAT ( @d, '%h'); --6SELECT FORMAT ( @d, 'h'); --NULL
--Invalid Format
Any
Other
Character
The character is copied to the result string unchanged.
Collapse | Copy Code
DECLARE @d DATETIMESET @d='5/9/2001 6:09:59 AM' SELECT FORMAT ( @d, ' invalid h'); -- invali9 6SELECT FORMAT ( @d, 'Qui\t in h \hour'); --Quit in 6 hour
Note : Use any combination of custom format string to customize your date and
time format requirements.
PARSE()
Converts string value to a requested data type. If convertion is not possible to the requested data
type then PARSE will throw an error. Use PARSE only for converting from string to date/time and
number types. For general type conversions, continue to use CAST or CONVERT. Keep in mind
that there is a certain performance overhead in parsing the string value. PARSE relies on the
presence of .the .NET Framework Common Language Runtime (CLR).
Syntax :-
Collapse | Copy Code
PARSE ( string_value AS data_type [ USING culture ] )
Where string_value is an nvarchar (4000) value representing the formatted value to parse into the
specified data type. string_value must be a valid representation of the requested data type. or
else PARSE raises an error. data_type is a literal value representing the data type requested for
the result. culture is an optional string that identifies the culture in which string_value is
formatted. If the culture argument is not provided, then the language of the current session is
used. If the culture argument is not valid then PARSE raises an error.
We can see some sample code for parsing date and or time.
Collapse | Copy Code
-- If the culture argument isn't provided, the language of current session is used.SELECT PARSE('04/26/2013 16:30:59' AS date) AS 'date'; -- 2013-04-26SELECT PARSE('04/26/2013 16:30:59' AS time) AS 'time'; -- 16:30:59.0000000SELECT PARSE('04/26/2013 16:30:59' AS datetime) AS 'datetime'; -- 2013-04-26
16:30:59.000SELECT PARSE('04/26/2013 16:30:59' AS smalldatetime) AS 'smalldatetime'; -- 2013-04-26 16:31:00SELECT PARSE('04/26/2013 16:30:59' AS datetime2) AS 'datetime2'; -- 2013-04-26 16:30:59.0000000SELECT PARSE('04/26/2013 16:30:59' AS datetimeoffset) AS 'datetimeoffset'; -- 2013-04-26 16:30:59.0000000 +00:00
SELECT PARSE('04/26/2013' AS date) AS 'date'; -- 2013-04-26SELECT PARSE('04/26/2013' AS time) AS 'time'; -- 00:00:00.0000000SELECT PARSE('04/26/2013' AS datetime) AS 'datetime'; -- 2013-04-26 00:00:00.000SELECT PARSE('04/26/2013' AS smalldatetime) AS 'smalldatetime'; -- 2013-04-26 00:00:00SELECT PARSE('04/26/2013' AS datetime2) AS 'datetime2'; -- 2013-04-26 00:00:00.0000000SELECT PARSE('04/26/2013' AS datetimeoffset) AS 'datetimeoffset'; -- 2013-04-26 00:00:00.0000000 +00:00
SELECT PARSE('16:30:59' AS date) AS 'date'; -- 2013-04-27SELECT PARSE('16:30:59' AS time) AS 'time'; -- 16:30:59.0000000SELECT PARSE('16:30:59' AS datetime) AS 'datetime'; -- 2013-04-27 16:30:59.000SELECT PARSE('16:30:59' AS smalldatetime) AS 'smalldatetime'; -- 2013-04-27 16:31:00SELECT PARSE('16:30:59' AS datetime2) AS 'datetime2'; -- 2013-04-27 16:30:59.0000000SELECT PARSE('16:30:59' AS datetimeoffset) AS 'datetimeoffset'; -- 2013-04-27 16:30:59.0000000 +00:00
-- Parse to datetime specifying culture in which string value is formattedSELECT PARSE('Friday, 26 April 2013' AS date USING 'en-US') AS 'date'; -- 2013-04-26SELECT PARSE('Friday, 26 April 2013' AS time USING 'en-US') AS 'time'; -- 00:00:00.0000000SELECT PARSE('Friday, 26 April 2013' AS datetime USING 'en-US') AS 'datetime'; -- 2013-04-26 00:00:00.000SELECT PARSE('Friday, 26 April 2013' AS smalldatetime USING 'en-US') AS 'smalldatetime'; -- 2013-04-26 00:00:00SELECT PARSE('Friday, 26 April 2013' AS datetime2 USING 'en-US') AS 'datetime2'; -- 2013-04-26 00:00:00.0000000SELECT PARSE('Friday, 26 April 2013' AS datetimeoffset USING 'en-US') AS 'datetimeoffset'; -- 2013-04-26 00:00:00.0000000 +00:00
SELECT PARSE('04/26/2013 16:30:59' AS date USING 'en-US') AS 'date'; -- 2013-04-26SELECT PARSE('04/26/2013 16:30:59' AS time USING 'en-US') AS 'time'; -- 16:30:59.0000000SELECT PARSE('04/26/2013 16:30:59' AS datetime USING 'en-US') AS 'datetime'; -- 2013-04-26 16:30:59.000SELECT PARSE('04/26/2013 16:30:59' AS smalldatetime USING 'en-US') AS 'smalldatetime'; --2013-04-26 16:31:00SELECT PARSE('Samstag, 27. April 2013' AS datetime2 USING 'de-DE') AS 'datetime2'; -- 2013-04-27 00:00:00.0000000SELECT PARSE('sábado, 27 de Abril de 2013' AS datetimeoffset USING 'pt-PT') AS 'datetimeoffset'; -- 2013-04-27 00:00:00.0000000 +00:00
-- PARSE throws error for non convertable valueSELECT PARSE('WRONGVALUE' AS date) AS 'date'; -- Error, 'Error converting string value 'WRONGVALUE' into data type date using culture ''.'
TRY_PARSE()
Converts string value to a requested data type. If convertion is not possible to the requested data
type then TRY_PARSE will return NULL. Use TRY_PARSE only for converting from string to
date/time and number types. For general type conversions, continue to use CAST or CONVERT.
Keep in mind that there is a certain performance overhead in parsing the string value. TRY_PARSE
relies on the presence of .the .NET Framework Common Language Runtime (CLR).
Syntax :-
Collapse | Copy Code
TRY_PARSE ( string_value AS data_type [ USING culture ] )
Parameters are similar to PARSE, All The example of PARSE() will work as it is except the below
one,
Collapse | Copy Code
SELECT TRY_PARSE('WRONGVALUE' AS date) AS 'date'; -- NULL
Sample :-
Collapse | Copy Code
--If the culture argument isn't provided, the language of current session is used.SELECT TRY_PARSE('Friday, 26 April 2013' AS date) AS 'date'; -- 2013-04-26-- Parse to datetime specifying culture in which string value is formattedSELECT TRY_PARSE('04/26/2013 16:30:59' AS smalldatetime USING 'en-US') AS 'smalldatetime'; --2013-04-26 16:31:00SELECT TRY_PARSE('Samstag, 27. April 2013' AS datetime2 USING 'de-DE') AS 'datetime2'; -- 2013-04-27 00:00:00.0000000SELECT TRY_PARSE('sábado, 27 de Abril de 2013' AS datetimeoffset USING 'pt-PT') AS 'datetimeoffset'; -- 2013-04-27 00:00:00.0000000 +00:00SELECT TRY_PARSE('Friday, 26 April 2013' AS datetimeoffset USING 'en-US') AS 'datetimeoffset'; -- 2013-04-26 00:00:00.0000000 +00:00-- PARSE throws error for non convertable valueSELECT TRY_PARSE('WRONGVALUE' AS date) AS 'date'; -- NULL
TRY_CONVERT()
TRY_CONVERT function is very similar to the CONVERT function except that it returns NULL when
the conversion cannot be completed.
Syntax :--
Collapse | Copy Code
TRY_CONVERT ( data_type [ ( length ) ], expression [, style ] )
TRY_CONVERT parameters are same to CONVERT function.
Sample :-
Collapse | Copy Code
SELECT TRY_CONVERT(date, 'WRONGVALUE') AS 'date' -- NULL
References
The reference links from MSDN are given below.
Conversion Functions - Transact-SQL
String Functions - Transact-SQL
Summary
In this article I have explained Functions that helps to convert date and time values to and from
string literals and other date and time formats. If I have missed anything or need any change in
definition then please let me know. I hope you have enjoyed this article and got some value
addition to your knowledge.
You might be interested in the below articles.
Date and Time Data Types and Functions - SQL Server.
Some Interesting Date and Time Questions.
String Data Types and Functions - SQL Server
.NET Framework Cultures with Culture Specific Formats and Mapping with SQL Server
Language.
I have put my time and efforts on all of my articles, Please don't forget to mark
your votes, suggestions and feedback to improve the quality of this and upcoming articles. Thanks
for reading.
License
This article, along with any associated source code and files, is licensed under The Code Project
Open License (CPOL)
Shemeer NSSoftware Developer (Senior)
India
Technology Specialist | CodeProject MVP | Visual Studio Gallery Contributor | Author | Geek |
Netizen | Husband | ChessPlayer
Most of my articles are listed on top 5 of the respective 'Best articles of the month' and some of
my articles are published on ASP.NET WebSite's Article of the Day section.
Check my contributions in Visual Studio Gallery and Code Project
Technical Blog: http://www.shemeerns.com
Facebook: http://facebook.com/shemeernsblog
Twitter : http://twitter.com/shemeerns
Google+ : http://google.com/+Shemeernsblog
Follow on Twitter Google
Search this forum Go
About the Author
Article Top
Comments and Discussions
You must Sign In to use this message board.
Profile popups Spacing Relaxed Noise Medium Layout Normal Per page 25
Update
First Prev Next
shafqatirfan 11-May-14 18:56
gvprabu 24-Sep-13 5:38
vamsi madasu 21-Aug-13 19:23
MohamedKamalPharm 18-Jun-13 13:41
Uday P.Singh 17-Jun-13 20:42
My Vote 5
My vote of 5
My vote of 5
My vote of 5
My vote of 5
Permalink | Advertise | Privacy | Mobile Web02 | 2.8.140526.1 | Last Updated 20 May 2013
Article Copyright 2013 by Shemeer NSEverything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid
manoj kumar choubey 15-Jun-13 1:15
Shemeer NS 15-Jun-13 21:53
Mihai MOGA 13-Jun-13 20:46
Shemeer NS 15-Jun-13 21:52
Monjurul Habib 12-Jun-13 9:56
Shemeer NS 12-Jun-13 10:01
Member 10037669 20-May-13 10:40
Shemeer NS 20-May-13 21:54
TechnoGeek001 17-May-13 17:57
Shemeer NS 19-May-13 5:03
Brian A Stephens 17-May-13 9:19
Shemeer NS 19-May-13 5:02
Member 10049488 14-May-13 8:54
Shemeer NS 14-May-13 9:07
Peter Hayward 13-May-13 17:09
Shemeer NS 13-May-13 20:44
LeonardoFiorot 13-May-13 3:51
Shemeer NS 13-May-13 20:43
gvprabu 8-May-13 1:52
Shemeer NS 13-May-13 20:43
Last Visit: 31-Dec-99 18:00 Last Update: 5-Jun-14 6:59 Refresh 1 2 Next »
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
My vote of 5
Re: My vote of 5
My vote of 5
Re: My vote of 5
My vote of 5
Re: My vote of 5
My vote of 4
Re: My vote of 4
My vote of 5
Re: My vote of 5
My vote of 4
Re: My vote of 4
5 OUT OF 5 100%
Re: 5 OUT OF 5 100%
My vote of 5
Re: My vote of 5
My vote of 5
Re: My vote of 5
My vote of 5
Re: My vote of 5