Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 format Date Column to MM/YYYY

Author  Topic 

hollyquinn
Starting Member

31 Posts

Posted - 2009-02-18 : 15:28:58
Hi I am trying to format a datetime column to the format MM/YYYY. I have figured out how to format it as MM/DD/YYYY, but not as just month and year. This is what I've got:

select SessionMonth = CONVERT(char(10), SessionMonth, 101) from tblSessionID

Can anyone help me? Thanks.

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-02-18 : 15:30:43
datepart or datename function might help you.

select convert(varchar,datepart(mm,getdate()))+'/'+convert(varchar,datepart(year,getdate()))
select datename(mm,getdate())+'/'+datename(year,getdate())
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-02-18 : 15:40:28
Also, There is no MM/YYYY type date format, you'll have to convert it to varchar for this.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2009-02-18 : 15:57:31
[code]
select [MM/YYYY] = right(convert(varchar(10),getdate(),103),7)


Results:

MM/YYYY
-------
02/2009

(1 row(s) affected)
[/code]

CODO ERGO SUM
Go to Top of Page

hollyquinn
Starting Member

31 Posts

Posted - 2009-02-18 : 16:12:04
quote:
Originally posted by Michael Valentine Jones


select [MM/YYYY] = right(convert(varchar(10),getdate(),103),7)


Results:

MM/YYYY
-------
02/2009

(1 row(s) affected)


CODO ERGO SUM



Hi Michael, how do you do what you are doing above on a Column instead of just the current date? Thanks.
Go to Top of Page

hollyquinn
Starting Member

31 Posts

Posted - 2009-02-18 : 16:13:32
quote:
Originally posted by Michael Valentine Jones


select [MM/YYYY] = right(convert(varchar(10),getdate(),103),7)


Results:

MM/YYYY
-------
02/2009

(1 row(s) affected)


CODO ERGO SUM



Oh nevermind! I got it! Replace MM/YYYY with my column name. Thanks so much. :)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-02-18 : 16:14:46
No, replace getdate() with your column name. [MM/YYYY] is just the alias he used for the formatted column in the result set.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -