Case when and else : Case « Select Query « SQL Server / T-SQL






Case when and else


1>
2> CREATE TABLE project   (project_no   CHAR(4) NOT NULL,
3>                         project_name CHAR(15) NOT NULL,
4>                         budget FLOAT NULL)
5>
6> insert into project values ('p1', 'Search Engine',        120000.00)
7> insert into project values ('p2', 'Programming',          95000.00)
8> insert into project values ('p3', 'SQL',                  186500.00)
9>
10> -- CASE Expressions
11>
12> SELECT project_name,
13>         CASE
14>          WHEN budget > 0 AND budget < 100000 THEN 1
15>          WHEN budget >= 100000 AND budget < 200000 THEN 2
16>          WHEN budget >= 200000 AND budget < 300000 THEN 3
17>          ELSE 4
18>         END budget_weight
19>        FROM project
20> GO

(1 rows affected)

(1 rows affected)

(1 rows affected)
project_name    budget_weight
--------------- -------------
Search Engine               2
Programming                 1
SQL                         2

(3 rows affected)
1>
2> drop table project
3> GO
1>
2>
           
       








Related examples in the same category

1.Select case statement
2.CASE: return a specified value based on a set of business logic
3.Use CASE in the UPDATE statement
4.'Case when' statement in a procedure
5.Case When with else
6.Case when NULL
7.Case when with Mod and ABS()
8.A simple CASE takes an expression that equates to a Boolean result