How do I convert a date like 2020-03-25 to 03-20?
I tried this:
select
FORMAT (d.[Date], 'MM-YY') as monthyear
from x d
But I am getting 03-YY
Why do I not get the year?
In SQL Server:
select format(d.[Date], 'MM-yy') as monthyear
from x d
I would personally suggest using CONVERT
with a style code over FORMAT
; FORMAT
can be a pretty slow function. The style 5
with the first 3 characters removed is what you're after:
SELECT STUFF(CONVERT(YT.[date],5),1,3,'') AS MonthYear
FROM dbo.YourTable YT;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.