Solved

SQL 2008 - need help always finding certain date range

Posted on 2014-09-26
8
148 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

 

Author Comment

by:doc_jay
ID: 40346363
Phil,
   
    Looks like 'EOMONTH' is only part of 2012 as well.  :(
0
 
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:Scott Pletcher
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 Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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