lankapala
asked on
T-sql Date Format
Hi ,
I need date format yyyy-mm or mm-yyyy
I used following sql but it's not working properly , any new idea
LEFT('0' + CAST(MONTH(mw.TransDate) as varchar(2)),2) + '-' + CAST(YEAR(mw.TransDate) as char(4))
I need date format yyyy-mm or mm-yyyy
I used following sql but it's not working properly , any new idea
LEFT('0' + CAST(MONTH(mw.TransDate) as varchar(2)),2) + '-' + CAST(YEAR(mw.TransDate) as char(4))
I suspect that your mw.Transdate is not a date column. With the date your expression works fine:
Check it working here
Check it working here
Replace GETDATE() with your column name
SELECT RIGHT(CONVERT(VARCHAR(10), GETDATE(), 105), 7) AS [MM-YYYY]
/*
Result
02-2017
*/
SELECT CONVERT(VARCHAR(7), GETDATE(), 120) AS [YYYY-MM]
/*
Result
2017-02
*/
ASKER
Thank you all the comments. Any idea about this
CONVERT(VARCHAR(7), mw.TransDate, 111)
Yes mw.TransDate is a date column
CONVERT(VARCHAR(7), mw.TransDate, 111)
Yes mw.TransDate is a date column
CONVERT(VARCHAR(7), mw.TransDate, 111) returns in YYYY/MM format
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
lankapala, a feedback will be appreciated.
Cheers
Cheers
ASKER
thanks
Open in new window