问题
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