-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29_Expressions.sql
More file actions
135 lines (123 loc) · 3.27 KB
/
29_Expressions.sql
File metadata and controls
135 lines (123 loc) · 3.27 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**************************************************************
* SQL Server 2022 Expressions Tutorial
* Description: This script demonstrates various Transact-SQL expressions:
* - CASE expressions (both simple and searched) for data categorization.
* - COALESCE to provide default values and concatenate strings.
* - NULLIF to avoid division by zero and compare values.
**************************************************************/
-------------------------------------------------
-- Region: 0. Initialization
-------------------------------------------------
/*
Ensure you are using the target database.
*/
USE TestDB;
GO
-------------------------------------------------
-- Region: 1. Creating and Populating Sample Table
-------------------------------------------------
/*
1.1 Create a sample table for demonstration.
*/
IF OBJECT_ID(N'dbo.SampleData', N'U') IS NOT NULL
DROP TABLE dbo.SampleData;
GO
CREATE TABLE dbo.SampleData
(
ID INT PRIMARY KEY,
Name NVARCHAR(100),
Age INT,
Salary DECIMAL(10, 2),
Department NVARCHAR(100)
);
GO
/*
1.2 Insert sample data into the table.
*/
INSERT INTO dbo.SampleData (ID, Name, Age, Salary, Department)
VALUES
(1, 'Alice', 30, 60000.00, 'HR'),
(2, 'Bob', 25, NULL, 'IT'),
(3, 'Charlie', 35, 80000.00, 'Finance'),
(4, 'David', 40, 90000.00, 'IT'),
(5, 'Eve', 28, NULL, 'HR');
GO
-------------------------------------------------
-- Region: 2. CASE Expressions
-------------------------------------------------
/*
2.1 Simple CASE expression to categorize age groups.
*/
SELECT
Name,
Age,
CASE
WHEN Age < 30 THEN 'Young'
WHEN Age BETWEEN 30 AND 39 THEN 'Adult'
ELSE 'Senior'
END AS AgeGroup
FROM dbo.SampleData;
GO
/*
2.2 Searched CASE expression to handle NULL values in Salary.
*/
SELECT
Name,
Salary,
CASE
WHEN Salary IS NULL THEN 'Salary not provided'
ELSE 'Salary provided'
END AS SalaryStatus
FROM dbo.SampleData;
GO
-------------------------------------------------
-- Region: 3. COALESCE Expressions
-------------------------------------------------
/*
3.1 Use COALESCE to provide a default value for NULL Salary.
*/
SELECT
Name,
COALESCE(Salary, 50000.00) AS Salary
FROM dbo.SampleData;
GO
/*
3.2 Use COALESCE to concatenate Name and Department with a fallback.
*/
SELECT
COALESCE(Name, 'Unknown') + ' - ' + COALESCE(Department, 'Unknown') AS EmployeeInfo
FROM dbo.SampleData;
GO
-------------------------------------------------
-- Region: 4. NULLIF Expressions
-------------------------------------------------
/*
4.1 Use NULLIF to avoid division by zero when calculating Salary per Year.
*/
SELECT
Name,
Salary,
Age,
Salary / NULLIF(Age, 0) AS SalaryPerYear
FROM dbo.SampleData;
GO
/*
4.2 Use NULLIF to compare columns and return NULL if they are equal.
*/
SELECT
Name,
Department,
NULLIF(Department, 'IT') AS NonITDepartment
FROM dbo.SampleData;
GO
-------------------------------------------------
-- Region: 5. Cleanup
-------------------------------------------------
/*
Clean up the sample table.
*/
DROP TABLE dbo.SampleData;
GO
-------------------------------------------------
-- End of Script
-------------------------------------------------