Skip to content

Commit a500eed

Browse files
committed
Fix #105 - Changed G-4220 examples to use constants instead of literals
1 parent 2663e3e commit a500eed

File tree

1 file changed

+33
-33
lines changed
  • docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce

1 file changed

+33
-33
lines changed

docs/4-language-usage/4-control-structures/2-case-if-decode-nvl-nvl2-coalesce/g-4220.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,52 @@
1111

1212
``` sql
1313
-- @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;
1919
```
2020

21-
## Example (good)
21+
`null` values can be compared in `decode`:
2222

2323
``` 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;
3530
```
3631

37-
## Example (bad)
32+
## Example (good)
3833

3934
``` 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;
4646
```
4747

48-
## Example (good)
48+
Simple `case` can not compare `null` values, instead the searched `case` expression must be used:
4949

5050
``` sql
5151
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
5858
else
59-
0
59+
constants_up.co_lang_not_supported
6060
end
61-
from dual;
61+
from countries ctry;
6262
```

0 commit comments

Comments
 (0)