Share via


Convert If Statement Into SQL Case Statement...

Question

Thursday, April 12, 2012 10:07 AM

I am trying to convert an if statement into a sql case statement so I can use it within a sql select statement...

Can anyone help me please?

If New_Inj.INJ1PersInj = 'Insured Vehicle - Policy Holder' THEN 
     If Claim_Details.ClmClmCompo = '1' THEN 
          'Yes' 
     ELSE 
          'No - ' + Claim_Details.ClmClmCompoReas
     End If
ELSE 
     If New_Inj.INJ1HBBAct = 'Definately Yes' AND Own_Solicitor_Details.Sols1InstSols <> '3' AND Own_Solicitor_Details.Sols1InstSols <> '4' AND Own_Solicitor_Details.Sols1WhoseSols <> '1' THEN 
          'Yes' 
     ELSE 
          'No'
     End IF
End If

All replies (3)

Thursday, April 12, 2012 10:12 AM ✅Answered

SELECT CASE

  WHEN New_Inj.INJ1PersInj = 'Insured Vehicle - Policy Holder' AND Claim_Details.ClmClmCompo = '1' THEN 'Yes' 

  WHEN New_Inj.INJ1PersInj = 'Insured Vehicle - Policy Holder' AND Claim_Details.ClmClmCompo <> '1' THEN 'No - ' + Claim_Details.ClmClmCompoReas 

  WHEN New_Inj.INJ1HBBAct = 'Definately Yes' AND Own_Solicitor_Details.Sols1InstSols <> '3' AND Own_Solicitor_Details.Sols1InstSols <> '4' AND Own_Solicitor_Details.Sols1WhoseSols <> '1' THEN 'Yes'

  ELSE 'No'

END

FROM MyTables


Thursday, April 12, 2012 10:24 AM ✅Answered

You can also try:

select 
    case when New_Inj.INJ1PersInj = 'Insured Vehicle - Policy Holder'
        then case when Claim_Details.ClmClmCompo = '1'
                 then 'Yes'
                 else 'No - ' + Claim_Details.ClmClmCompoReas
             end
        else case when New_Inj.INJ1HBBAct = 'Definately Yes' AND 
                       Own_Solicitor_Details.Sols1InstSols <> '3' AND 
                       Own_Solicitor_Details.Sols1InstSols <> '4' AND 
                       Own_Solicitor_Details.Sols1WhoseSols <> '1'
                 then 'Yes' 
                 else 'No'
             end
    end
from MyTable

I hope this helps.

 


Thursday, April 12, 2012 11:05 AM

Cheers guys, I came up with something similar but it wasn't functioning properly. Tweaked slightly and working now :)