Solved

SQL 2008 - need help always finding certain date range

Posted on 2014-09-26
8
149 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
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 …
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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