Overblog
Edit post Follow this blog Administration + Create my blog

CASE Statement in Update Queries

September 7 2009 , Written by Balavardhan Reddy Published on #SQL Server2005

We can implement CASE statement in Update Queries. Instead of multiple update queries, you can implement multile condition by CASE statement to update a table or get the customized result.

Ex : We have a table called Tmp_Tab with column as (PRIORITY,IsUpdated ,IsNeedtoDelete)

Current Data In Tmp_Tab

PRIORITY       IsUpdated       IsNeedtoDelete
HIGH               YES               NO
LOW                NO                1
HIGH               0                 YES
0                  NO                1


Execute the Below Update  statement :

UPDATE Tmp_Tab SET PRIORITY =
                        (CASE
                            WHEN PRIORITY = 'HIGH' THEN '0'
                            WHEN PRIORITY = 'MEDIUM' THEN '1'
                            WHEN PRIORITY = 'LOW' THEN '2'
                            ELSE PRIORITY  END
                             ),
                      IsPremium =
                        (CASE
                            WHEN IsUpdated   = 'YES' THEN '1'
                            WHEN IsUpdated   = 'NO' THEN '0'
                            ELSE IsUpdated   END
                             ),
                      IsSubscription =
                        (CASE
                            WHEN IsNeedtoDelete = 'YES' THEN '1'
                            WHEN IsNeedtoDelete = 'NO' THEN '0'
                            ELSE IsNeedtoDelete END
                             )



After Execution of the Query

PRIORITY               IsUpdated               IsNeedtoDelete
0                        1                          0
2                        0                          1
0                        0                          1
0                        0                          1
Share this post
Repost0
To be informed of the latest articles, subscribe:
Comment on this post
D
<br /> Great Post, I’ll be definitely coming back to your site. Keep the nice work up.<br /> <br /> dissertation help<br /> <br /> <br />
Reply
D
<br /> Thanks, was looking forward to such info, what took you so long for sharing it. Hope to see such informative stuff in future as well.<br /> <br /> dissertations<br /> <br /> <br />
Reply
A
<br /> Useful and helpful web developing ideas and tips shared with informative details.<br /> <br /> <br />
Reply
L
<br /> <br /> 1.Hello U there, that was good thinking. I wait for more of this stuff. By Jack Amiss, a logo design services provider.<br /> <br /> <br /> <br />
Reply
L
<br /> <br /> 1.Hello U there, that was good thinking. I wait for more of this stuff. By Jack Amiss, a logo design services provider.<br /> <br /> <br /> <br />
Reply