Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL CASE WHEN or IF ELSE IF

Tags:

sql

I have multiple conditions to meet in a case I would like to Know if I can use > < instead of defining every case

In this case when the credit rating is smaller then 3 then, the word "NO TRADE" will be inserted and larger then 3, smaller then 5 would be "POOR", and so on and so on

SELECT  ClientId, 
    FirstName,
    LastName,
    Gender,
    DateOfBirth,
    CreditRating,

        CASE CreditRating

            WHEN 0 THEN 'NO TRADE'
            WHEN 1 THEN 'NO TRADE'
            WHEN 2 THEN 'NO TRADE'
            WHEN 3 THEN 'POOR'
            WHEN 4 THEN 'POOR'
            WHEN 5 THEN 'AVARAGE'
            WHEN 6 THEN 'AVARAGE'
            WHEN 7 THEN 'GOOD'
            ELSE 'PERFECT' 

            END AS RATING

    FROM dbo.client
like image 355
Gerrit Duvenage Avatar asked Sep 06 '14 07:09

Gerrit Duvenage


1 Answers

Sure it is possible.

CASE 

WHEN CreditRating <= 2  THEN 'NO TRADE'
WHEN CreditRating  <= 4 THEN 'POOR'
WHEN CreditRating  <= 6 THEN 'AVARAGE'
WHEN CreditRating  = 7 THEN 'GOOD'

ELSE 'PERFECT' 

END AS RATING
like image 182
Wrapper Tech Avatar answered Oct 03 '22 22:10

Wrapper Tech