Display date and time using month name in T-SQL

The table has a field that is a date type. The format of this field is mm / dd / yy. Is there any way to convert it to dd-Month name-yy?

Regards,

+3
source share
5 answers

Without any problems, you can use CONVERT to get the format "dd MONTHNAME yyyy":

SELECT CONVERT(VARCHAR, GETDATE(), 106)

eg. January 25, 2010

If you need your exact format, you may need to do a bit of manual tinkering, for example:

SELECT CAST(DAY(GETDATE()) AS VARCHAR) +  '-' + LEFT(DATENAME(mm, GETDATE()), 3) + '-' + RIGHT(CAST(YEAR(GETDATE()) AS VARCHAR), 2)

eg. "25-Jan-10"

Update:
In fact, a shorter way to achieve this:

SELECT REPLACE(CONVERT(VARCHAR, GETDATE(), 6), ' ', '-')

eg. "25-Jan-10"

+7
source

:

select convert( char(8), getdate(), 5 ) 

:

--

Source: compuspec date format conversion

+2
source

can you try convert () ?

+1
source

You may need to distinguish between storing date and time values ​​in SQL Server and displaying them. Here's a great article explaining what's going on behind the scenes: The Ultimate Guide to datetime datatypes

0
source

Source: https://habr.com/ru/post/1729875/


All Articles