?
Solved

SQL 2008 - need help always finding certain date range

Posted on 2014-09-26
8
Medium Priority
?
151 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
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 

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 2000 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 49

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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

752 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