Link to home
Start Free TrialLog in
Avatar of lankapala
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))
Avatar of Jeff Tennessen
Jeff Tennessen
Flag of United States of America image

Try this:

CONVERT(varchar(7), mw.TransDate, 121)

Open in new window

Avatar of chaau
I suspect that your mw.Transdate is not a date column. With the date your expression works fine:

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
*/

Open in new window

Avatar of lankapala
lankapala

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) returns in YYYY/MM format
ASKER CERTIFIED SOLUTION
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
lankapala, a feedback will be appreciated.
Cheers
thanks