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
 SQL Server 2008 Forums
 Analysis Server and Reporting Services (2008)
 Using Format to change date display

Author  Topic 

towardabettercountry
Starting Member

26 Posts

Posted - 2012-05-08 : 15:31:00
I'm trying to get today last year (e.g. today it would be May 5, 2012). Among many things, here is one thing I've tried:

=DateAdd("yyyy",-1,Format(Today(),"MMMM dd, yyyy"))

However, that gives me "5/8/2011 12:00:00 AM". I need it in this format: May 5, 2012. I've tried using FormatDateTime, but that gives an error. Any help is appreciated!

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-05-08 : 15:51:05
quote:
Originally posted by towardabettercountry

I'm trying to get today last year (e.g. today it would be May 5, 2012). Among many things, here is one thing I've tried:

=DateAdd("yyyy",-1,Format(Today(),"MMMM dd, yyyy"))

However, that gives me "5/8/2011 12:00:00 AM". I need it in this format: May 5, 2012. I've tried using FormatDateTime, but that gives an error. Any help is appreciated!

Do the formatting after the DATEADD.
=Format(DateAdd("yyyy",-1,Today()),"MMMM dd, yyyy")
Go to Top of Page

towardabettercountry
Starting Member

26 Posts

Posted - 2012-05-08 : 15:55:14
That works perfect! Man I spent forever on that... Thanks for the help!

Edited for question: How does putting FORMAT on the outside make it after the DATEADD? Just trying to learn!
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-05-08 : 19:22:55
quote:
Originally posted by towardabettercountry

That works perfect! Man I spent forever on that... Thanks for the help!

Edited for question: How does putting FORMAT on the outside make it after the DATEADD? Just trying to learn!

In my mind I was thinking of the precedence of operations. The expression within the innermost brackets gets evaluated first, and goes outward from there. So, I visualize it as SSRS evaluating DateAdd("yyyy",-1,Today()) first and evaluating the Format function AFTER that.
Go to Top of Page
   

- Advertisement -