incorrect syntax within DATEDIFF

  • Disclaimer: I still feel like a newbie so please go easy.

    I extracted the TSQL code out of our database that another programmer used to figure out the age of a patient.

    age_calc = cast(

    case

    when (datediff(dd,vsm040.bth_ts, vpm300.adm_ts) >= 365) then

    convert(integer,(datediff(dd,vsm040.bth_ts, vpm300.adm_ts) / 365.25))

    else

    datediff(dd,vsm040.bth_ts, vpm300.adm_ts) / 365.25

    end as int(4))

    The above code works fine and gives me an age. When I tried to copy this code to get the length of stay for an inpatient under 150 days I keep getting a syntax error. Here is the code I'm trying to use....

    los = cast(

    case

    when (DateDiff(dd,vpm300.adm_ts, Isnull(vpm300.dschrg_ts,getdate()) < 150) then

    convert(integer,(DateDiff(day,vpm300.adm_ts, Isnull(vpm300.dschrg_ts,getdate())))

    else

    '0'

    end as int(4))

    The error I'm getting is....

    Server: Msg 170, Level 15, State 1, Line 24

    Line 24: Incorrect syntax near '<'.

    Line 24 is the same line as ".....< 150....."

    bth_ts = birthdate

    adm_ts = admission date and time

    dschrg_ts = discharge date and time

    When I use the line....

    DateDiff(dd,vpm300.adm_ts, Isnull(vpm300.dschrg_ts,getdate()))

    ...I get a valid length of stay but I'm trying not to include a length of stay on recurring accounts (outpatient accounts) that discharge after 365 days. I picked 150 out of the air but I know we've had a few 60-70 day stays in the last year.

    Thanks in advance,

    John

  • select

    case when DateDiff(dd,vpm300.adm_ts, Isnull(vpm300.dschrg_ts,getdate())) < 150

    then

    DateDiff(day, vpm300.adm_ts, Isnull(vpm300.dschrg_ts, getdate()))

    else 0

    end

    from vpm300

    You don't need to cast to an int. DateDiff() returns an int. Not sure how "not to include a length of stay on recurring accounts" fits in.

  • That may have worked. I'll check it for sure tomorrow. I just didn't get why copying previous code that worked doesn't work....I'm not looking for an answer why right now because I think yours worked and that's all I'm going for....a working report.

    Not sure how "not to include a length of stay on recurring accounts" fits in.

    We have different patient types setup in our system. My report is pulling up recurring accounts with a length of stay of 365 days but I don't want that coming up in the column. I only want accounts with the type of Inpatient to have a length of stay show up > 0 where as all other types should have a LOS of 0. Using the " < 150 " (and the ELSE part of that statement) would keep recurring accounts LOS at 0.

    Not sure if that made sense but anyway.....thanks for the reply.

    John

Viewing 3 posts - 1 through 2 (of 2 total)

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