Date format using CONVERT function in SQL Server

I’ve not posted here in a while but I’m going to make an effort to post things more frequently which I find useful in my every day job. Hopefully I can then help others and also look back at this when I forget it again!

I was using the CONVERT function recently to change a date in SQL to a VARCHAR. This would work fine but it would give me a default view like this

SELECT CONVERT(VARCHAR(50), GETDATE()) AS DateString

Result: Oct 3 2012 2:15PM

I realise this is a very basic example but it is just to show how the CONVERT function will behave. To resolve this I had to use an overload for this function which includes a “date style”. So the SQL statement becomes

SELECT CONVERT(VARCHAR(50), GETDATE(), 103) AS DateString 

Result: 03/10/2012

I used 103 as the style because this matched the format I was looking for. You can simply change this depending on format.

To find a full list of formats you can visit http://www.sql-server-helper.com/tips/date-formats.aspx. This is where I found this information, it is none of my own but I wanted to share this with you.

Thanks, hopefully this can help others!

Leave a Reply