convert date yyyy-mm-dd to mmm-yy SQL Server 2016

随声附和 提交于 2021-02-17 05:20:25

问题


I want to convert date yyyy-mm-dd (stored as a date format) to mmm-yy format.

There are no exact matches in the prior questions on the site.

I have tried substrings and convert function, was considering creating a scalar function but its taking me a while and hoping someone has an easy solution.


回答1:


You can construct the format using string operations:

select left(datename(month, datecol), 3) + '-' + right(datename(year, datecol), 2)

Or using format():

select format(datecol, 'MMM-yy')



回答2:


Try this

select replace(right(convert(varchar(9), getdate(), 6), 6), ' ', '') asDate


来源:https://stackoverflow.com/questions/60948207/convert-date-yyyy-mm-dd-to-mmm-yy-sql-server-2016

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!