Skip to content

Commit

Permalink
Fix #105 - Changed G-4220 examples to use constants instead of literals
Browse files Browse the repository at this point in the history
  • Loading branch information
kibeha committed Jul 14, 2022
1 parent 2663e3e commit a500eed
Showing 1 changed file with 33 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,52 @@

``` sql
-- @formatter:off
select decode(dummy, 'X', 1
, 'Y', 2
, 'Z', 3
, 0)
from dual;
select decode(ctry.country_code, constants_up.co_ctry_uk, constants_up.co_lang_english
, constants_up.co_ctry_fr, constants_up.co_lang_french
, constants_up.co_ctry_de, constants_up.co_lang_german
, constants_up.co_lang_not_supported)
from countries ctry;
```

## Example (good)
`null` values can be compared in `decode`:

``` sql
select case dummy
when 'X' then
1
when 'Y' then
2
when 'Z' then
3
else
0
end
from dual;
-- @formatter:off
select decode(ctry.country_code, constants_up.co_ctry_uk, constants_up.co_lang_english
, constants_up.co_ctry_fr, constants_up.co_lang_french
, null , constants_up.co_lang_unknown
, constants_up.co_lang_not_supported)
from countries ctry;
```

## Example (bad)
## Example (good)

``` sql
-- @formatter:off
select decode(dummy, 'X', 1
, 'Y', 2
, null, -1
, 0)
from dual;
select case ctry.country_code
when constants_up.co_ctry_uk then
constants_up.co_lang_english
when constants_up.co_ctry_fr then
constants_up.co_lang_french
when constants_up.co_ctry_de then
constants_up.co_lang_german
else
constants_up.co_lang_not_supported
end
from countries ctry;
```

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

``` sql
select case
when dummy = 'X' then
1
when dummy = 'Y' then
2
when dummy is null then
-1
when ctry.country_code = constants_up.co_ctry_uk then
constants_up.co_lang_english
when ctry.country_code = constants_up.co_ctry_fr then
constants_up.co_lang_french
when ctry.country_code is null then
constants_up.co_lang_unknown
else
0
constants_up.co_lang_not_supported
end
from dual;
from countries ctry;
```

0 comments on commit a500eed

Please sign in to comment.