Overblog Follow this blog
Edit post Administration Create my blog
Balavardhan Reddy Narani

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

Repost 0

Comment on this post

dissertation help 12/01/2009 09:56


Great Post, I’ll be definitely coming back to your site. Keep the nice work up.

dissertation help


dissertation 12/01/2009 09:50


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.

dissertations


Ashish 11/26/2009 13:19


Useful and helpful web developing ideas and tips shared with informative details.


logo design services 11/02/2009 13:17



1.Hello U there, that was good thinking. I wait for more of this stuff. By Jack Amiss, a logo design services provider.



logo design services 11/02/2009 12:08



1.Hello U there, that was good thinking. I wait for more of this stuff. By Jack Amiss, a logo design services provider.