Solved

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

Posted on 2014-07-23
3
295 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
[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
  • 2
3 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40215123
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
ID: 40215169
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:
Scott Pletcher earned 500 total points
ID: 40215232
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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.

696 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