|
11 | 11 |
|
12 | 12 | ``` sql |
13 | 13 | -- @formatter:off |
14 | | -select decode(dummy, 'X', 1 |
15 | | - , 'Y', 2 |
16 | | - , 'Z', 3 |
17 | | - , 0) |
18 | | - from dual; |
| 14 | +select decode(ctry.country_code, constants_up.co_ctry_uk, constants_up.co_lang_english |
| 15 | + , constants_up.co_ctry_fr, constants_up.co_lang_french |
| 16 | + , constants_up.co_ctry_de, constants_up.co_lang_german |
| 17 | + , constants_up.co_lang_not_supported) |
| 18 | + from countries ctry; |
19 | 19 | ``` |
20 | 20 |
|
21 | | -## Example (good) |
| 21 | +`null` values can be compared in `decode`: |
22 | 22 |
|
23 | 23 | ``` sql |
24 | | -select case dummy |
25 | | - when 'X' then |
26 | | - 1 |
27 | | - when 'Y' then |
28 | | - 2 |
29 | | - when 'Z' then |
30 | | - 3 |
31 | | - else |
32 | | - 0 |
33 | | - end |
34 | | - from dual; |
| 24 | +-- @formatter:off |
| 25 | +select decode(ctry.country_code, constants_up.co_ctry_uk, constants_up.co_lang_english |
| 26 | + , constants_up.co_ctry_fr, constants_up.co_lang_french |
| 27 | + , null , constants_up.co_lang_unknown |
| 28 | + , constants_up.co_lang_not_supported) |
| 29 | + from countries ctry; |
35 | 30 | ``` |
36 | 31 |
|
37 | | -## Example (bad) |
| 32 | +## Example (good) |
38 | 33 |
|
39 | 34 | ``` sql |
40 | | --- @formatter:off |
41 | | -select decode(dummy, 'X', 1 |
42 | | - , 'Y', 2 |
43 | | - , null, -1 |
44 | | - , 0) |
45 | | - from dual; |
| 35 | +select case ctry.country_code |
| 36 | + when constants_up.co_ctry_uk then |
| 37 | + constants_up.co_lang_english |
| 38 | + when constants_up.co_ctry_fr then |
| 39 | + constants_up.co_lang_french |
| 40 | + when constants_up.co_ctry_de then |
| 41 | + constants_up.co_lang_german |
| 42 | + else |
| 43 | + constants_up.co_lang_not_supported |
| 44 | + end |
| 45 | + from countries ctry; |
46 | 46 | ``` |
47 | 47 |
|
48 | | -## Example (good) |
| 48 | +Simple `case` can not compare `null` values, instead the searched `case` expression must be used: |
49 | 49 |
|
50 | 50 | ``` sql |
51 | 51 | select case |
52 | | - when dummy = 'X' then |
53 | | - 1 |
54 | | - when dummy = 'Y' then |
55 | | - 2 |
56 | | - when dummy is null then |
57 | | - -1 |
| 52 | + when ctry.country_code = constants_up.co_ctry_uk then |
| 53 | + constants_up.co_lang_english |
| 54 | + when ctry.country_code = constants_up.co_ctry_fr then |
| 55 | + constants_up.co_lang_french |
| 56 | + when ctry.country_code is null then |
| 57 | + constants_up.co_lang_unknown |
58 | 58 | else |
59 | | - 0 |
| 59 | + constants_up.co_lang_not_supported |
60 | 60 | end |
61 | | - from dual; |
| 61 | + from countries ctry; |
62 | 62 | ``` |
0 commit comments