SQL Case Statement Syntax? *

Question

What is the complete and correct syntax for the SQL Case statement?

Answer

The complete syntax depends on the database engine you're working with:

For SQL Server:

  CASE case-expression
      WHEN when-expression-1 THEN value-1
    [ WHEN when-expression-n THEN value-n ... ]
[ ELSE else-value ]
  END

or:

  CASE
      WHEN boolean-when-expression-1 THEN value-1
    [ WHEN boolean-when-expression-n THEN value-n ... ]
[ ELSE else-value ]
  END

expressions, etc:

case-expression    - something that produces a value
when-expression-x  - something that is compared against the case-expression
value-1            - the result of the CASE statement if:
                         the when-expression == case-expression
                      OR the boolean-when-expression == TRUE
boolean-when-exp.. - something that produces a TRUE/FALSE answer

Link: CASE (Transact-SQL)

Also note that the ordering of the WHEN statements is important. You can easily write multiple WHEN clauses that overlap, and the first one that matches is used.

Note: If no ELSE clause is specified, and no matching WHEN-condition is found, the value of the CASE expression will be NULL.

< br > via < a class="StackLink" href=" http://stackoverflow.com/questions/4622/" >SQL Case Statement Syntax?< /a>
Share on Google Plus

About Cinema Guy

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment