CASE help for newbie

  • Hi all,

    Looking to get some help with the following that I just can't get the case statement right with.....

    CONVERT(VARCHAR(10), z.Date, 105) as VisitDate,
    case
    when v.acc_Visit_Frequency = '1' then CONVERT(VARCHAR(10), dateadd(m,12,z.date), 105)
    when v.acc_Visit_Frequency = '2' then CONVERT(VARCHAR(10), dateadd(m,6,z.date), 105)
    when v.acc_Visit_Frequency = '3' then CONVERT(VARCHAR(10), dateadd(m,4,z.date), 105)
    when v.acc_Visit_Frequency = '4' then CONVERT(VARCHAR(10), dateadd(m,3,z.date), 105)
    else null
    end as DateDue,

    I think it may be due to using the same field 'z.date' twice but appreciate any help.

    Thanks

  • What's the problem that you're getting?  What are you seeing?  What are you expecting / hoping to see?

    Thomas Rushton
    blog: https://thelonedba.wordpress.com

  • Error?

    ------------------------------------------------------------------------------------------------Standing in the gap between Consultant and ContractorKevin3NFDallasDBAs.com/BlogWhy is my SQL Log File HUGE?!?![/url]The future of the DBA role...[/url]SQL Security Model in Plain English[/url]

  • rob.king - Thursday, January 12, 2017 6:16 AM

    Hi all,

    Looking to get some help with the following that I just can't get the case statement right with.....

    CONVERT(VARCHAR(10), z.Date, 105) as VisitDate,
    case
    when v.acc_Visit_Frequency = '1' then CONVERT(VARCHAR(10), dateadd(m,12,z.date), 105)
    when v.acc_Visit_Frequency = '2' then CONVERT(VARCHAR(10), dateadd(m,6,z.date), 105)
    when v.acc_Visit_Frequency = '3' then CONVERT(VARCHAR(10), dateadd(m,4,z.date), 105)
    when v.acc_Visit_Frequency = '4' then CONVERT(VARCHAR(10), dateadd(m,3,z.date), 105)
    else null
    end as DateDue,

    I think it may be due to using the same field 'z.date' twice but appreciate any help.

    Thanks

    Looks fine. What's it not doing that it should be doing?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Sorry - getting invalid column name 'date' on the four case lines

  • rob.king - Thursday, January 12, 2017 6:26 AM

    Sorry - getting invalid column name 'date' on the four case lines

    Case-sensitive database?  Try changing 'date' to 'Date'.

    Thomas Rushton
    blog: https://thelonedba.wordpress.com

  • What are the names of the columns in the table aliased as 'z'? Can you post the rest of the query and the CREATE TABLE statement?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • You're a star - didn't spot that!!

    Thank you v.much

  • ThomasRushton - Thursday, January 12, 2017 6:27 AM

    rob.king - Thursday, January 12, 2017 6:26 AM

    Sorry - getting invalid column name 'date' on the four case lines

    Case-sensitive database?  Try changing 'date' to 'Date'.

    nice catch 🙂

    ------------------------------------------------------------------------------------------------Standing in the gap between Consultant and ContractorKevin3NFDallasDBAs.com/BlogWhy is my SQL Log File HUGE?!?![/url]The future of the DBA role...[/url]SQL Security Model in Plain English[/url]

  • Kevin3NF - Thursday, January 12, 2017 6:36 AM

    ThomasRushton - Thursday, January 12, 2017 6:27 AM

    rob.king - Thursday, January 12, 2017 6:26 AM

    Sorry - getting invalid column name 'date' on the four case lines

    Case-sensitive database?  Try changing 'date' to 'Date'.

    nice catch 🙂

    Thanks.  Been bitten by that a few times in the past...  :/

    Thomas Rushton
    blog: https://thelonedba.wordpress.com

Viewing 10 posts - 1 through 9 (of 9 total)

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