-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcase.html
More file actions
43 lines (39 loc) · 1.73 KB
/
case.html
File metadata and controls
43 lines (39 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<!-- SECCIÓN CASE -->
<div id="case-section">
<div id="adsense-container" style="width: 350px; position: absolute; left: 0px;"></div>
<div class="inner-modal">
<div class="inner" style="padding: 0px;">
<h1>SQL CASE Statement</h1>
<p>The CASE statement goes through conditions and returns a value when the first condition is met (like an
if-then-else statement). Once a condition is true, it will stop reading and return the result. If no
conditions are true, it returns the value in the ELSE clause.</p>
<p>If there is no ELSE part and no conditions are true, it returns NULL.</p>
<h2>CASE Syntax</h2>
<pre><code class="language-sql">CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;</code></pre>
<h2>SQL CASE Examples</h2>
<p>The following SQL goes through conditions and returns a value when the first condition is met:</p>
<pre><code class="language-sql">SELECT patient_id, height,
CASE
WHEN height > 175 THEN 'height is greater than 175'
WHEN height = 175 THEN 'height is 175'
ELSE 'height is under 175'
END AS height_group
FROM patients;</code></pre>
<h2>CASE in ORDER BY Example</h2>
<p>The following SQL will order the patients by allergies. However, if allergies is NULL, then order by
patient_id:</p>
<pre><code class="language-sql">SELECT patient_id, first_name, allergies
FROM patients
ORDER BY
(CASE
WHEN allergies IS NULL THEN first_name
ELSE allergies
END);</code></pre>
</div>
</div>
</div>