CASE has two formats: 1. Simple CASE function - Compares an expression to determine the result. 2. Searched CASE Function: CASE WHEN Boolean_expression THEN Result ELSE result_expression END 1. Simple CASE function evaluates input_expression and find the match with when_expression. If not found, it will return the ELSE result if an ELSE clause is specifi ed, or a NULL value if no EL
CASE has two formats: 1. Simple CASE function - Compares an expression to determine the result. 2. Searched CASE Function: CASE WHEN Boolean_expression THEN Result ELSE result_expression END 1. Simple CASE function evaluates input_expression and find the match with when_expression. If not found, it will return the ELSE result if an ELSE clause is specifi ed, or a NULL value if no EL
Direitos autorais:
Attribution Non-Commercial (BY-NC)
Formatos disponíveis
Baixe no formato TXT, PDF, TXT ou leia online no Scribd
CASE has two formats: 1. Simple CASE function - Compares an expression to determine the result. 2. Searched CASE Function: CASE WHEN Boolean_expression THEN Result ELSE result_expression END 1. Simple CASE function evaluates input_expression and find the match with when_expression. If not found, it will return the ELSE result if an ELSE clause is specifi ed, or a NULL value if no EL
Direitos autorais:
Attribution Non-Commercial (BY-NC)
Formatos disponíveis
Baixe no formato TXT, PDF, TXT ou leia online no Scribd
ORDER BY CASE WHEN Address = 'USA' THEN 1 WHEN Address = 'Ahmedabad' THEN 2 WHEN Address = 'Gujarat' THEN 3 ELSE 4 END
CASE has two formats:
1. Simple CASE Function - Compares an expression to determine the result. 2. Searched CASE Function - Evaluates a set of Boolean expressions to determine the result. CASE Syntax 1. Simple CASE function: CASE input_expression WHEN when_expression THEN Result ELSE result_expression END 2. Searched CASE function: CASE WHEN Boolean_expression THEN Result ELSE result_expression END 1. Simple CASE Function Evaluates input_expression and find the match with when_expression. If found, it will return the Result and if not found, it will return the ELSE result_express ion if an ELSE clause is specified, or a NULL value if no ELSE clause is specifi ed. DECLARE @Type varchar(20) SET @Type = 'Programming' SELECT CASE @Type WHEN 'Sql' THEN 'sqltutorials.blogspot.com' WHEN 'Programming' THEN 'programmingschools.blogspot.com' WHEN 'Travel' THEN 'travelyourself.blogspot.com' ELSE 'Not yet categorized' END Value = programmingschools.blogspot.com If SET @Type = 'Picture', then Return value = Not yet categorized
2.Searched CASE Function
Evaluates Boolean_expression for each WHEN clause and returns result_expression of the first Boolean_expression that evaluates to TRUE. If no Boolean_expression evaluates to TRUE, SQL Server returns the ELSE result_e xpression if an ELSE clause is specified, or a NULL value if no ELSE clause is s pecified. DECLARE @Price integer SET @Price = (20-9) SELECT CASE WHEN @Price IS NULL THEN 'Not yet priced' WHEN @Price < color="#ff0000">THEN 'Very Reasonable Price' WHEN @Price >= 10 AND @Price < color="#ff0000">THEN 'Reasonable Price' ELSE 'Expensive book!' END Value = Reasonable Price If SET @Price = (30-1), then return Value = Expensive book!
CREATE PROCEDURE CustomerGetFinal
@CustomerTypeID int AS BEGIN SELECT CustomerID, FirstName, LastName, CustomerTypeID FROM Customer WHERE CustomerTypeID = @CustomerTypeID ORDER BY CASE WHEN @CustomerTypeID = 1 THEN (RANK() OVER (ORDER BY FirstName, Last Name)) WHEN @CustomerTypeID = 2 THEN (RANK() OVER (ORDER BY LastName, First Name)) WHEN @CustomerTypeID = 3 THEN (RANK() OVER (ORDER BY LastName DESC, FirstName DESC)) END END