Solved

SQL Server - How to pull data for October of Previous Year

Posted on 2014-07-23
3
274 Views
Last Modified: 2014-07-23
Below is my SQL so far:

SELECT DISTINCT
D.Account,
R.Account as RAccount,
D.MRN,
D.AdmitDate,
D.[Discharge Date],
R.AdmitDate as RAdmitDate,
R.[Discharge Date] as RDischargeDate,
DATEDIFF(day, D.[Discharge Date], R.ADMITDATE) as DaysSince,
R.AdmitDX AS RAdmitDx,--#4
Dx.Description as RAdmitDxDesc --#4
FROM  
Encounter_ALL_ADM D WITH (NOLOCK)
      LEFT OUTER JOIN Encounter_ALL_ADM R WITH (NOLOCK)
      ON D.MRN = R.MRN --AND
      AND D.Account <> R.Account
        --ON D.EID = R.EID
      --AND D.Account = R.Account
      --AND D.Institution = 'SMMC'
      AND R.PatientType = 'IN'
      AND (R.ADMITDATETIME - D.DISCHARGEDATETIME) BETWEEN 0 and 30
        LEFT OUTER JOIN Dictionary_Diagnosis_Code Dx --#4
        ON Dx.Code = R.AdmitDX --#4
        --AND Dx.Code = D.AdmitDX
        AND Dx. [EFFECTIVE_DATE] >= '01-OCT-2013'
   
WHERE
D.AccountStatus_PatientType IN ('DIS_IN')
AND D.DISCHARGEDATETIME between DATEADD(mm, -12, GetDate())and DATEADD(mm, 0, GetDate())
AND D.PatientType = 'IN'


Question - Where it says 'Dx. [EFFECTIVE_DATE] >= '01-OCT-2013'' - I need to pull the data as of October 1 of previous year. No matter when the report is run
0
Comment
Question by:rustypoot
  • 2
3 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
AND Dx. [EFFECTIVE_DATE] >= DATEADD(MONTH, 9, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0))

Btw:
AND D.DISCHARGEDATETIME >= DATEADD(mm, -12, GetDate()) AND D.DISCHARGEDATETIME < DATEADD(mm, 0, GetDate())
*
0
 

Author Comment

by:rustypoot
Comment Utility
Thanks Scott! Month should be 10 right?

Dx. [EFFECTIVE_DATE] >= DATEADD(MONTH, 10, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0))

Not sure what you mean for the SQL below!

Btw:
AND D.DISCHARGEDATETIME >= DATEADD(mm, -12, GetDate()) AND D.DISCHARGEDATETIME < DATEADD(mm, 0, GetDate())
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
The year will start on 01/01 (Jan 01), so you need to add 9 months to get to month 10/October :-).

Sorry, very little time available, so I wasn't clear.  I was suggesting using that code instead of your original code using BETWEEN.  BETWEEN is not recommended for use with date/datetime.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

9 Experts available now in Live!

Get 1:1 Help Now