SQL Case Statement Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • goal2007
    New Member
    • Jul 2007
    • 25

    SQL Case Statement Problem

    Here is what i am doing:

    select case
    when SUBSTRING(task_ name, 1, 3)= 'PTO'
    then Replace(task_na me,'PTO','PTO_H oliday')
    when SUBSTRING(task_ name, 1, 7)= 'Holiday'
    then Replace(task_na me,'Holiday','P TO_Holiday')

    when SUBSTRING(proj_ name, 1, 9)= '9900-2831'
    then Replace(proj_na me,'9900-2831','II Internal')
    when SUBSTRING(proj_ name, 1, 9)= '9900-2788'
    then Replace(proj_na me,'9900-2788','II Internal')


    Line 3 ELSE proj_name,task_ name END as Task_Name




    FROM dbo._Employee_T ime_Detail_Repo rts INNER JOIN
    dbo._vw_Employe e ON dbo._Employee_T ime_Detail_Repo rts.res_name = dbo._vw_Employe e.Employee

    This work fine, however in line THREE i need to show two columns

    something like this

    ELSE proj_name,task_ name END as Task_Name,Proje ct_Name

    but this is worng. how can i fix that
  • rob313
    New Member
    • Sep 2007
    • 16

    #2
    You need to use two CASE statements like this:

    select case
    when SUBSTRING(task_ name, 1, 3)= 'PTO'
    then Replace(task_na me,'PTO','PTO_H oliday')
    when SUBSTRING(task_ name, 1, 7)= 'Holiday'
    then Replace(task_na me,'Holiday','P TO_Holiday')
    ELSE task_name
    END as task_name
    CASE
    when SUBSTRING(proj_ name, 1, 9)= '9900-2831'
    then Replace(proj_na me,'9900-2831','II Internal')
    when SUBSTRING(proj_ name, 1, 9)= '9900-2788'
    then Replace(proj_na me,'9900-2788','II Internal')
    ELSE proj_name
    END as proj_name
    FROM ....

    Comment

    • rob313
      New Member
      • Sep 2007
      • 16

      #3
      Opps, I forgot a comma in between the two case statements.

      select case
      when SUBSTRING(task_ name, 1, 3)= 'PTO'
      then Replace(task_na me,'PTO','PTO_H oliday')
      when SUBSTRING(task_ name, 1, 7)= 'Holiday'
      then Replace(task_na me,'Holiday','P TO_Holiday')
      ELSE task_name
      END as task_name,

      CASE
      when SUBSTRING(proj_ name, 1, 9)= '9900-2831'
      then Replace(proj_na me,'9900-2831','II Internal')
      when SUBSTRING(proj_ name, 1, 9)= '9900-2788'
      then Replace(proj_na me,'9900-2788','II Internal')
      ELSE proj_name
      END as proj_name
      FROM ....

      Comment

      • goal2007
        New Member
        • Jul 2007
        • 25

        #4
        Thank you SOOO much for your help

        Comment

        Working...