Date and Time Conversions Using SQL Server
Date and Time Conversions Using SQL Server
com/) MENU
Problem
There are many instances when dates and times don't show up at your doorstep in the format you'd like it to be, nor does the
output of a query fit the needs of the people viewing it. One option is to format the data in the application itself. Another option is
to use the built-in functions SQL Server provides to format the date string for you.
Solution
SQL Server provides a number of options you can use to format a date/time string. One of the first considerations is the actual
date/time needed. The most common is the current date/time using getdate(). This provides the current date and time according
to the server providing the date and time. If a universal date/time is needed, then getutcdate() should be used. To change the
format of the date, you convert the requested date to a string and specify the format number corresponding to the format needed.
Below is a list of formats and an example of the output. The date used for all of these examples is "2006-12-30 00:38:54.840".
You can also format the date or time without dividing characters, as well as concatenate the date and time string:
If you want to get a list of all valid date and time formats, you could use the code below and change the @date to GETDATE() or
any other date you want to use. This will output just the valid formats.
/
DECLARE @counter INT = 0
DECLARE @date DATETIME = '2006-12-30 00:38:54.840'
Recommended Reading
Continue your learning on SQL Server dates with these tips:
Determine SQL Server Date and Time Parts with DATEPART and DATENAME Functions (/sqlservertip/2507/determine-sql-
server-date-and-time-parts-with-datepart-and-datename-functions/)
SQL Server Date and Time Data Types (/sqlservertip/1616/sql-server-2008-date-and-time-data-types/)
SQL Server function to convert integer date to datetime format (/sqlservertip/1712/sql-server-function-to-convert-integer-date-
to-datetime-format/)
SQL Server DateTime Best Practices (/sqlservertip/5206/sql-server-datetime-best-practices/)
Format SQL Server Dates with FORMAT Function (/sqlservertip/2655/format-sql-server-dates-with-format-function/)
Next Steps
The formats listed above are not inclusive of all formats provided. Experiment with the different format numbers to see what
others are available.
These formats can be used for all date/time functions, as well as data being served to clients, so experiment with these data
format conversions to see if they can provide data more efficiently.
Also, check out the SQL Server FORMAT Function to Format Dates (/sqlservertip/2655/format-sql-server-dates-with-format-
function/).
/
Related Resources
Date and Time Conversions Using SQL Server... (/sqlservertip/1145/date-and-time-conversions-using-sql-server/)
Format SQL Server Dates with FORMAT Function... (/sqlservertip/2655/format-sql-server-dates-with-format-function/)
Determine SQL Server Date and Time Parts with DATE... (/sqlservertip/2507/determine-sql-server-date-and-time-parts-with-
datepart-and-datename-functions/)
SQL Server 2008 Date and Time Data Types... (/sqlservertip/1616/sql-server-2008-date-and-time-data-types/)
SQL Server function to convert integer date to dat... (/sqlservertip/1712/sql-server-function-to-convert-integer-date-to-datetime-
format/)
More Database Developer Tips... (/sql-server-developer-resources/)