In the world of Application and DB Development there is always a need to display dates in a format that is understandable to your audience. For example, in the US we normally expect to see a date as mm/dd/yyyy; however, in Great Britain, the normal format is dd/mm/yyyy. The Convert function in SQL Server provides a means to translate SQL Server’s default date format to just about any format.
The syntax for the Convert function is as follows:
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
--
Available Date Formats --
Source Code
It shows some of the different ways dates can be formatted and produces the result below.
Below are a few resources that I found on using the Convert function.
- Working with SQL Server Date/Time Variables – DataBase Journal
- Working with SQL Server Date/Time Variables: Part Two – Displaying Dates and Times in Different Formats – DataBase Journal
- What the format codes mean in SQL ‘CONVERT’ – DBForums
- HOWTO – Compare a date string to datetime in SQL Server? – StackOverflow
- How to convert DateTime to VarChar – StackOverflow
- Sql Server string to date conversion – StackOverflow
I also posted a while back about Handling Dates in SQL Server.
–
Happy Learning!
[...] Converting Dates in SQL Server – Rhonda Tipton explains the CONVERT syntax for T-SQL for folks who need to convert between different date formats like MM/DD/YYYY and DD/MM/YYYY. [...]
By: SQL Server and Cloud Links for the Week | Brent Ozar - SQL Server DBA on January 23, 2009
at 6:31 am