Solved

SQL 2008 - need help always finding certain date range

Posted on 2014-09-26
8
135 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

930 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

10 Experts available now in Live!

Get 1:1 Help Now