1
0
Fork 0
mirror of https://github.com/treffynnon/sqlstyle.guide.git synced 2025-03-09 12:49:51 -05:00

Add case statements section

This commit is contained in:
apurvis@lumoslabs.com 2016-04-07 19:10:37 -04:00
parent 0495c287af
commit fb6eac76ed

View file

@ -280,6 +280,35 @@ WHERE r.last_name IN
AND c.confirmed = 'Y'); AND c.confirmed = 'Y');
``` ```
#### Case statements (PostGres)
`CASE` and `END` should have the same left justification.
`WHEN`/`THEN` should be indented the same as the `ELSE`/`value`.
```sql
SELECT CASE WHEN x > y AND x < z
THEN 'x more than y but less than z'
WHEN x > y AND x > z
THEN 'x more than y and more than z'
ELSE
'x and y not related'
END AS city
FROM office_locations
```
#### Case statements (MySql)
```sql
SELECT CASE postcode
WHEN 'BN1'
THEN 'Brighton'
WHEN 'EH1'
THEN 'Edinburgh'
END AS city
FROM office_locations
```
### Preferred formalisms ### Preferred formalisms
* Make use of `BETWEEN` where possible instead of combining multiple statements * Make use of `BETWEEN` where possible instead of combining multiple statements
@ -292,9 +321,10 @@ WHERE r.last_name IN
likely should be. likely should be.
```sql ```sql
SELECT CASE postcode SELECT CASE postcode WHEN 'BN1'
WHEN 'BN1' THEN 'Brighton' THEN 'Brighton'
WHEN 'EH1' THEN 'Edinburgh' WHEN 'EH1'
THEN 'Edinburgh'
END AS city END AS city
FROM office_locations FROM office_locations
WHERE country = 'United Kingdom' WHERE country = 'United Kingdom'