SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Grabbing data for last month
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

meef
Yak Posting Veteran

90 Posts

Posted - 08/14/2012 :  13:03:33  Show Profile  Reply with Quote
How would I set an expression up to grab data from the previous calendar month when it's run?

I tried this for the first day of last month:

DateAdd(DateInterval.Month, DateDiff(DateInterval.Month, "1/1/1900", Today())-2, "1/1/1900")

And this for the last day:

DateAdd(DateInterval.Day,-1*DatePart(DateInterval.Day,Today()),Today())

But it's giving me a syntax error. What is the proper way to do this?

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

Posted - 08/14/2012 :  13:11:21  Show Profile  Visit nigelrivett's Homepage  Reply with Quote
dateadd(mm,datediff(mm,0,getdate()),0) will give the first day of the current month
so first day of last month is
dateadd(mm,datediff(mm,0,getdate()-1),0)
and the last day is
dateadd(mm,datediff(mm,0,getdate()),0)-1

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47189 Posts

Posted - 08/14/2012 :  13:15:48  Show Profile  Reply with Quote
are you asking for t-sql or ssrs expression?

t-sql expression will be like

SELECT DATEADD(mm,DATEDIFF(mm,0,GETDATE())-1,0) AS StartofprevMonth,
DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)-1 AS EndOfPrevMonth


ssrs you will do like


=DateAdd(DateInterval.Month,DateDiff(DateInterval.Month, #1/1/1900#, Now())-1,#1/1/1900#)
=DateAdd(DateInterval.Day,-1,DateAdd(DateInterval.Month,DateDiff(DateInterval.Month, #1/1/1900#, Now()),#1/1/1900#))


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


Edited by - visakh16 on 08/14/2012 13:17:41
Go to Top of Page

meef
Yak Posting Veteran

90 Posts

Posted - 08/16/2012 :  08:21:48  Show Profile  Reply with Quote
Thanks guys. This was initially going to be set up in the report unless I couldn't get that going, then I would have put it in the stored procedure. The expression for the report worked.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000