17 Ağustos 2009 Pazartesi

CASE (Transact-SQL)


Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END



input_expression

Is the expression evaluated when the simple CASE format is used. input_expression is any valid expression.

WHEN when_expression

Is a simple expression to which input_expression is compared when the simple CASE format is used. when_expression is any valid expression. The data types of input_expression and eachwhen_expression must be the same or must be an implicit conversion.

THEN result_expression

Is the expression returned when input_expression equals when_expression evaluates to TRUE, or Boolean_expression evaluates to TRUE. result expression is any valid expression.

ELSE else_result_expression

Is the expression returned if no comparison operation evaluates to TRUE. If this argument is omitted and no comparison operation evaluates to TRUE, CASE returns NULL. else_result_expression is any valid expression. The data types of else_result_expression and any result_expression must be the same or must be an implicit conversion.

WHEN Boolean_expression

Is the Boolean expression evaluated when using the searched CASE format. Boolean_expression is any valid Boolean expression.



Select Name, StandardCost, ListPrice, (ListPrice - StandardCost)/StandardCost * 100,

case

when (ListPrice - StandardCost)/StandardCost * 100 <>

when (ListPrice - StandardCost)/StandardCost * 100 between 50 and 99 then 'Not Bad'

else 'Superb'

End [Profit]

from Product


SELECT ProductNumber, Name, 'Price Range' =

CASE

WHEN ListPrice = 0 THEN 'Mfg item - not for resale'

WHEN ListPrice <>

WHEN ListPrice >= 50 and ListPrice <>

WHEN ListPrice >= 250 and ListPrice <>

ELSE 'Over $1000'

END

FROM Production.Product

ORDER BY ProductNumber


Select AddressID, AddressLine1,

case

When AddressLine1 like '%Street%' or AddressLine1 like '%St.%' or

AddressLine1 like '%Ave%' or AddressLine1 like '%Rue%' then 1

else 0

End IsStreet

from Address

Hiç yorum yok:

Yorum Gönder