Mimi
Mimi

Reputation: 389

Case statement query

Can I use case statement on a field to change the value of another field? Something like that,

SELECT  TaskDescription, 
CASE 
When TaskDescription='Lab Dips / Handloom Sent' then seq ='1'  
When TaskDescription='Lab Dips / Handloom Approve' then seq ='2' end,seq FROM SomeTable

I have some description of tasks in TaskDescription column, that I want to fetch in a certain order. That's why I want to add a seq number to each task description value so that I can get the expected order just writing 'order by seq'

Upvotes: 1

Views: 27

Answers (1)

Iraj
Iraj

Reputation: 1522

if you want update value of table , you can use this :

Update SomeTable
Set seq = '1'
Where TaskDescription='Lab Dips / Handloom Sent'

Update :

Select TaskDescription ,seq 
From (
      SELECT  TaskDescription, 
            (CASE 
             When TaskDescription='Lab Dips / Handloom Sent' then 1 
             When TaskDescription='Lab Dips / Handloom Approve' then 2
           end) As seq FROM SomeTable
     ) As TB 
Order by TB.seq

Or :

With tb As (
    SELECT  TaskDescription
           ,(CASE TaskDescription
             When 'Lab Dips / Handloom Sent' then 1 
             When 'Lab Dips / Handloom Approve' then 2
          end) As seq 
     FROM SomeTable
)
Select * from tb
Order by tb.seq

Upvotes: 1

Related Questions