Solved

SQL 2008 - need help always finding certain date range

Posted on 2014-09-26
8
132 Views
Last Modified: 2014-09-26
Hi,

    I need SQL to run a report each month on the 25th that searches for rows that have a 'study_datetime' from the 25th of the last month to the 24th of the current month.

I was using this last month and it worked great...

select
DATEADD(month, datediff(month, 0, getdate()) -1, 24) as '24th'
, DATEADD(month, datediff(month, -1, getdate()), -7) as '25th'

Open in new window


..but this month it finds 8/25 to 9/25.  I needed it to find 8/25 to 9/24, and I need this to happen each month.  So, next month I will need it to find 9/25 to 10/24.  

Is there anyway to make this calculate this each month without me having to make a change once a month to the job I have setup that sends an email to people with a count?

Here is the all of the code I was using in my Job.

select
(select COUNT (*)
from dbo.study
where study_custom1 like '%HOSP1%'
and study_datetime >= DATEADD(month, datediff(month, 0, getdate()) -1, 24)
and study_datetime < DATEADD(month, datediff(month, -1, getdate()), -7)) as HOSP1, 
(select COUNT (*) 
from dbo.study
where study_custom1 like '%HOSP2%'
and study_datetime >= DATEADD(month, datediff(month, 0, getdate()) -1, 24)
and study_datetime < DATEADD(month, datediff(month, -1, getdate()), -7)) as HOSP2, 
(select COUNT (*) 
from dbo.study
where study_custom1 like '%HOSP3%'
and study_datetime >= DATEADD(month, datediff(month, 0, getdate()) -1, 24)
and study_datetime < DATEADD(month, datediff(month, -1, getdate()), -7)) as HOSP3

Open in new window


thank you
0
Comment
Question by:doc_jay
8 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40346333
select DATEFROMPARTS(year(getdate()),month(getdate())-1,25), DATEFROMPARTS(year(getdate()),month(getdate()),24)

Open in new window

0
 

Author Comment

by:doc_jay
ID: 40346343
Phil,

   is 'DATEFROMPARTS' a function of 2008 R2 or only 2012?
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40346352
It appears to be only 2012.

Here's a 2008 version answer:

select dateadd(day,25,eomonth(getdate(),-2)),dateadd(day,24,eomonth(getdate(),-1))

Open in new window

0
 

Author Comment

by:doc_jay
ID: 40346363
Phil,
   
    Looks like 'EOMONTH' is only part of 2012 as well.  :(
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40346377
If at first you don't succeed, try again.

Then give up!

Or give it a third go:

select dateadd(month,-1,dateadd(day,25-day(getdate()),getdate())), dateadd(day,24-day(getdate()),getdate())

Open in new window

0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40346384
It should be noted that the above answer, and I think your code, gives the answer as a datetime.

That may cause problems if you are comparing with a datetime, which I think you are.

Therefore, maybe you need:

and study_datetime >= convert(date,dateadd(month,-1,dateadd(day,25-day(getdate()),getdate())))
and study_datetime < convert(date,dateadd(day,25-day(getdate()),getdate())) as HOSP1

Open in new window

0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40346498
select
    sum(case when study_custom1 like '%HOSP1%' then 1 else 0 end) as HOSP1,
    sum(case when study_custom1 like '%HOSP2%' then 1 else 0 end) as HOSP2,
    sum(case when study_custom1 like '%HOSP3%' then 1 else 0 end) as HOSP3
from dbo.study
where
    --this isn't needed if study_custom1 always includes HOSP1/2/3
    (study_custom1 like '%HOSP1%' or study_custom1 like '%HOSP2%' or study_custom1 like '%HOSP3%') and
    study_datetime >= DATEADD(month, datediff(month, 0, getdate()) - 1, 24) and
    study_datetime <  DATEADD(month, datediff(month, 0, getdate()), 24)


Note that the second comparison is "<"; therefore, the date computation should return the 25th of the current month, as < day 25 is everything through all of day 24 :-) .
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40347233
I'd like to stress Scott's last point immediately above. You need BOTH dates to be the 25th of their respective months

If you don't you will be ignoring 1 day of data every month

(no points pl)
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now