header-blog
Sybase Case Statement PDF Print E-mail
Written by Patrick H. Piper   

The Sybase Case statement is handy for performing conditional SQL Expressions.  Recently, I needed to summarize the number of static host objects in the VitalQIP database using Sybase.  I needed to summarize them by counting how many statically defined objects by:

'A' or Address only records

'PTR' or Reverse PTR only records

'A' and 'PTR' records

The easiest way to do that was to use the Sybase Case Statement. The syntax for the Case Statement is as follows:

CASE 
    WHEN search_condition THEN expression 
   [WHEN search_condition THEN expression]...
   [ELSE expression]
END
 
CASE AND VALUES syntax:
 
CASE expression
    WHEN expression THEN expression
   [WHEN expression THEN expression]...
   [ELSE expression]
END

The Case Statement used to get my data looked like this:

SELECT COUNT(obj_id),
    CASE
        WHEN ns_update_flags = 1 THEN 'A'
        WHEN ns_update_flags = 2 THEN 'PTR'
        ELSE 'BOTH'
    END
FROM obj_prof
WHERE ns_usage = 1
GROUP BY ns_update_flags
 

When the SQL is executed, the output will look like this:

COUNT NS_UPDATE_FLAGS
878 BOTH
12 A
1 PTR
187224 BOTH


Add this page to your favorite Social Bookmarking websites

Digg! Del.icio.us! Google! Live! Facebook! StumbleUpon! TwitThis

Last Updated on Tuesday, 26 January 2010 12:13
 

Add comment


Security code
Refresh

Connect With Netlinx

There are a number of other ways you can connect with Netlinx, Inc. on the web. We have a presence on the following social network sites:

aim_32  delicious_32  googletalk_32  linkedin_32  twitter_32  yahoo_32  skype_32