Blog
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog
Report Abuse Form
Reason:    
 

Home >> Database >> CASE statement in SQL Server
CASE statement in SQL Server

In this blog I have tried to explain how to use CASE statement in SELECT statement of SQL Server.
Views: 925     Comments: 0
by Haider M Rizvi on 3/30/2012

The SQL Server CASE statement is a conditional statement that returns a single value based on the evaluation of a statement. CASE expressions can be used in SQL anywhere an expression can be used like SELECT list, WHERE clause, HAVING clause, IN list, DELETE and UPDATE statements. CASE statement can also be nested. This provides a lot of pliability for evaluating multiple expressions.

SQL Case statement can be used in 2 forms:

1.       SQL CASE statement with simple expression to compare and get results.

2.       SQL CASE statement with search or comparison expression to get results.

·        SQL CASE statement with simple expression to compare and get results

A simple CASE expression operates by comparing the first expression to the expression in each WHEN clause for equivalency.

Syntax


CASE expression

WHEN expression1 THEN expression1

[[WHEN expression2 THEN expression2] [...]]

[ELSE expressionN]

END

Example


SELECT CASE [Section]

      WHEN 1 THEN 'One'

      WHEN 2 THEN 'Two'

      WHEN 3 THEN 'Three'

      WHEN 4 THEN 'Four'

      WHEN 5 THEN 'Five'

      ELSE 'Greater than Five'

END as [Sec_Word] FROM [Table_Test]

·        SQL CASE statement with search or comparison expression to get results

A search or comparison CASE expression allows comparison operators, and the use of AND and/or OR between each Boolean expression.

Syntax


CASE

WHEN Boolean_expression1 THEN expression1

[[WHEN Boolean_expression2 THEN expression2] [...]]

[ELSE expressionN]

END

Example

SELECT CASE 

      WHEN [Section] < 5 THEN 'Less than Five'

      WHEN [Section] = 5 THEN 'Equal to Five'

      WHEN [Section] > 5 THEN 'Greater than Five'

END as [Sec_Word] FROM [Table_Test]

Title :  
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
       
 
Report Abuse Form
Reason:    
 
Latest ArticleRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest Blogs by Haider M RizviRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 5928
  
Copyright © 2013MindStick. All Rights Reserved.