We help IT Professionals succeed at work.

Previous month start and end dates

fahVB
fahVB asked
on
483 Views
Last Modified: 2010-05-18
I have several update stmts where i am manually entering a previous month start and end date
e.g
and v.effective between '06/01/2008' and '06/30/2008'

i have this stmt which actually calls the current month start and end date,

Declare @StartDate datetime
declare @EndDate datetime
set @startdate = dateadd(dd,1 - datepart(dd,getdate()),getdate())
set @enddate = dateadd(dd,- datepart(dd,dateadd(mm,1,getdate())),dateadd(mm,1,getdate()))
select @StartDate, @EndDate

my question is how should i change the above so it calls previous month start and end dates, so i can just place this in my update stmts

e.g.
and v.effective between @StartDate and @EndDate

Thank you...




Comment
Watch Question

DBAduck - Ben MillerSQL Server Architect
CERTIFIED EXPERT

Commented:
You would just use -1 instead of 1 for the dateadd(mm, ...) statements.

Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
So, after declaring these variables, I should be able to use them in my 100's of update stmt's, Correct?

Author

Commented:
Thank you...
DBAduck - Ben MillerSQL Server Architect
CERTIFIED EXPERT

Commented:
Yes, that is the point of the variables.  Very simple and effective.

Author

Commented:
Thank you...

Author

Commented:
I am getting diff resluts when i use hard coded date verses variables

Declare @StartDate datetime
declare @EndDate datetime
set @startdate = dateadd(m,-1,dateadd(dd,1 - datepart(dd,getdate()),getdate()) )
set @enddate = dateadd(d,-1,dateadd(dd,1 - datepart(dd,getdate()),getdate()))
select count(*) from coopi
where (card_acceptor_term_id not like 'CK%' OR card_acceptor_term_id not like '82320%')
and local_txn_date between @StartDate and @EndDate
and transaction_amount <> 0
and tran_fee_amount <> 0

I get 22537


but when i use this
select count(*) from coopi
where (card_acceptor_term_id not like 'CK%' OR card_acceptor_term_id not like '82320%')
and local_txn_date between '6/1/2008' and '6/30/2008'
and transaction_amount <> 0
and tran_fee_amount <> 0

i get 23287


is there i am missing?



DBAduck - Ben MillerSQL Server Architect
CERTIFIED EXPERT

Commented:
Remember that when you use getdate() that you get the time as well.  So you may want to do something like:

Declare @StartDate datetime
declare @EndDate datetime
declare @CurrentDate datetime
SET @CurrentDate = CONVERT(datetime, CONVERT(varchar, getdate(), 110))

set @startdate = dateadd(m,-1,dateadd(dd,1 - datepart(dd, @CurrentDate), @CurrentDate) )
set @enddate = dateadd(d,-1,dateadd(dd,1 - datepart(dd, @CurrentDate), @CurrentDate))

Then you will only get the actual date with 12:00 AM or 0:00

Author

Commented:
Beautiful, thanks so much ...Is there a way to assign points to a closed question?

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.