How can I format datetime in varchar using dd-MMM-yyyy format?

I believe this should be a simple solution, but it's hard for me to find anything on it ...

So: how do I get from datetimeto varcharwith dd-MMM-yyyy format?

Eg: my_date --> '29-May-2012'

The closure I managed to find

convert(varchar(20),my_date,105) --> '29-05-2012'

Yes, I need to do this correctly in T-SQL.

+5
source share
2 answers

Bet you can't wait for SQL Server 2012 (it might still be the right answer for other readers):

SELECT FORMAT(GETDATE(), 'dd-MMM-yyyy');

At the same time, this format is not supported initially, but from http://databases.aspfaq.com/database/what-are-the-valid-styles-for-converting-datetime-to-string.html the closest is probably the following :

SELECT REPLACE(CONVERT(CHAR(11), GETDATE(), 106), ' ', '-');
+11
source

:

SELECT replace(convert(char(11), getdate(), 113), ' ', '-')

SELECT replace(convert(char(11), getdate(), 106), ' ', '-')
+3

All Articles