Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server equivalent of MySQL DATE_FORMAT()

Hope we're having a good day and all set for Christmas.

Got a quick question. I'm converting a MySQL function into SQL Server, I've got most of the function converted except for one part which has the following:

@description = CONCAT(@description, date_format(@INLastUpdated, '%H:%i %d %b %Y'))

What I'm trying to do is to basically recreate the date_format function to format the date in the same way specified, but I'm not sure how to do it. from what I've seen in the MySQL documentation the format selected would give hour:minute day / short month name / year.

Anyone got any ideas?

like image 319
denimknight Avatar asked Aug 31 '25 04:08

denimknight


1 Answers

You can try this:

DECLARE @description VARCHAR(1000) = 'test'
DECLARE @INLastUpdated DATETIME = '2012-12-21 14:32:22'
SET @description = @description + ' ' 
    + LEFT(CONVERT(VARCHAR(8), @INLastUpdated, 8), 5) + ' ' 
    + CONVERT(VARCHAR(20), @INLastUpdated, 106)

SELECT @description

But be careful as format 106 depends on local language settings. Read more on MSDN

like image 68
Ivan Golović Avatar answered Sep 02 '25 19:09

Ivan Golović