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
Solved

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

Posted on 2014-07-23
3
291 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: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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Columnstore Indexes - real-time operational analytics 1 38
SQL 2008 with .NET 4.5.2 4 35
SQL Server Count where two id types exist in column 8 29
sql 2014,  lock limit 5 36
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, …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

789 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