How to subtract dates from previous date on same patient?

  • I can't take credit for the function. I had that in useful scripts stored on my machine. But its definitely from a blog online.

    The rest was me though 🙂

    MCITP SQL 2005, MCSA SQL 2012

  • Folks. This works like a charm.

    Thanx.

  • RTaylor2208 (6/23/2015)


    I can't take credit for the function. I had that in useful scripts stored on my machine. But its definitely from a blog online.

    The rest was me though 🙂

    I think everyone ends up writing one of those workday scripts at one time or another, myself included. It's how I got my start writing on SQL Server Central...

    http://qa.sqlservercentral.com/articles/Advanced+Querying/calculatingworkdays/1660/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Hi. Here's a quick script that returns all dates between two dates provided and whether that date is a weekend or not. you could use this in your scenario as you deem fit. Hope this helps.

    --

    DECLARE @startDate DATETIME='02/02/2015' -- mm/dd/yyyy

    DECLARE @endDate DATETIME= GETDATE() -- mm/dd/yyyy

    ;WITH Calender (CalanderDate, dayOfTheWeek) AS

    (

    SELECT @startDate AS CalanderDate, datepart(weekday,@startDate) as dayOfTheWeek

    UNION ALL

    SELECT CalanderDate + 1, datepart(weekday,CalanderDate + 1) as dayOfTheWeek FROM Calender

    WHERE CalanderDate + 1 <= @endDate

    )

    SELECT [Date] = CONVERT(VARCHAR(10),CalanderDate,25),

    (case when dayOfTheWeek in (1,7) then 'WeekEnd' else 'NotAWeekend' end) [IsWeekEnd]

    FROM Calender

    OPTION (MAXRECURSION 0)

  • sachin.patke (6/24/2015)


    Hi. Here's a quick script that returns all dates between two dates provided and whether that date is a weekend or not. you could use this in your scenario as you deem fit. Hope this helps.

    --

    DECLARE @startDate DATETIME='02/02/2015' -- mm/dd/yyyy

    DECLARE @endDate DATETIME= GETDATE() -- mm/dd/yyyy

    ;WITH Calender (CalanderDate, dayOfTheWeek) AS

    (

    SELECT @startDate AS CalanderDate, datepart(weekday,@startDate) as dayOfTheWeek

    UNION ALL

    SELECT CalanderDate + 1, datepart(weekday,CalanderDate + 1) as dayOfTheWeek FROM Calender

    WHERE CalanderDate + 1 <= @endDate

    )

    SELECT [Date] = CONVERT(VARCHAR(10),CalanderDate,25),

    (case when dayOfTheWeek in (1,7) then 'WeekEnd' else 'NotAWeekend' end) [IsWeekEnd]

    FROM Calender

    OPTION (MAXRECURSION 0)

    Ah... be careful now. That's not actually "quick". That uses a recursive CTE that counts and, even for a small number of rows, performance and resource usage is comparatively horrible. Please see the following article for why such things should be avoided even for a small number of rows.

    http://qa.sqlservercentral.com/articles/T-SQL/74118/

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)
    Intro to Tally Tables and Functions

  • Thanx for the articles.

Viewing 6 posts - 16 through 20 (of 20 total)

You must be logged in to reply to this topic. Login to reply