Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetime display using month name in T-SQL

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

Best Regards,

like image 993
Yongwei Xing Avatar asked May 17 '26 23:05

Yongwei Xing


1 Answers

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

SELECT CONVERT(VARCHAR, GETDATE(), 106)

e.g. "25 Jan 2010"

If you want your exact format, you may need a bit of manual fiddling around like:

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

e.g. "25-Jan-10"

Update:
In fact, a shorter way to achieving this is:

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

e.g. "25-Jan-10"

like image 77
AdaTheDev Avatar answered May 20 '26 16:05

AdaTheDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!