10,654,106 members (47,959 online) Sign in home quick answers discussions features community help Search 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 Server Functions, SQL Server Functions that helps to convert date and time values to and from string literals and other date and time 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 each morning. 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
23
Embed
SQL Server Functions That Helps to Convert Date and Time Values to and From String Literals and Other Date and Time Formats
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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"
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
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