[go: up one dir, main page]

0% found this document useful (0 votes)
36 views86 pages

07 Row Level Functions

The document provides an overview of SQL functions, categorizing them into single-row and multi-row functions, including string, numeric, aggregate, and date & time functions. It details various string functions such as CONCAT, UPPER, LOWER, and TRIM, as well as numeric and date functions like DATEPART, DATEDIFF, and FORMAT. The document serves as a guide for understanding and utilizing different SQL functions effectively.

Uploaded by

Sohail Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views86 pages

07 Row Level Functions

The document provides an overview of SQL functions, categorizing them into single-row and multi-row functions, including string, numeric, aggregate, and date & time functions. It details various string functions such as CONCAT, UPPER, LOWER, and TRIM, as well as numeric and date functions like DATEPART, DATEDIFF, and FORMAT. The document serves as a guide for understanding and utilizing different SQL functions effectively.

Uploaded by

Sohail Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 86

SQL FUNCTIONS

Baraa Khatib Salkini


YouTube | DATA WITH BARAA
SQL Course | SQL Functions
What are
Functions?

SQL Course | SQL Functions


Nested Function

SQL Course | SQL Functions


SQL
Runs outer
Functions
query

Single-Row Multi-Row
Functions Functions

String Numeric Aggregate Window


Functions Functions Functions Functions
(Basics) (Advanced)
Date & Time NULL
Functions Functions

SQL Course | SQL Functions


SQL
Runs outer
Functions
query

Single-Row Multi-Row
Functions Functions

String Numeric Aggregate Window


Functions Functions Functions Functions
(Basics) (Advanced)
Date & Time NULL
Functions Functions

SQL Course | SQL Functions


STRING
FUNCTIONS

Baraa Khatib Salkini


YouTube | DATA WITH BARAA
SQL Course | String Functions
String
Functions

Manipulation String
Calculation
Extraction

CONCAT LEN LEFT

UPPER RIGHT

LOWER SUBSTRING

TRIM

REPLACE

SQL Course | String Functions


CONCAT Combines multiple strings into one

UPPER Converts all characters to uppercase

LOWER Converts all characters to lowercase

TRIM Removes Leading and Trailing spaces

String REPLACE Replaces specific character with a new character


Functions
LEN Counts how many characters

LEFT Extracts specific Number of Characters from the start

RIGHT Extracts specific Number of Characters from the End

Substring Extracts a part of string at a specified position


CONCAT

SQL Course | String Functions | CONCAT


LOWER & UPPER

SQL Course | String Functions | LOWER & UPPER


TRIM

SQL Course | String Functions | TRIM


REPLACE

SQL Course | String Functions | REPLACE


LEN

SQL Course | String Functions | LEN


LEFT & RIGHT

SQL Course | String Functions | LEFT & RIGHT


SUBSTRING

SQL Course | String Functions | SUBSTRING


NUMERIC
FUNCTIONS

Baraa Khatib Salkini


YouTube | DATA WITH BARAA
SQL Course | Number Functions
SQL
Runs outer
Functions
query

Single-Row Multi-Row
Functions Functions

String Numeric Aggregate Window


Functions Functions Functions Functions
(Basics) (Advanced)
Date & Time NULL
Functions Functions

SQL Course | Numeric Functions


SQL Course | Numeric Functions
DATE & TIME
FUNCTIONS

Baraa Khatib Salkini


YouTube | DATA WITH BARAA
SQL Course | Date & Time Funcitons
SQL
Runs outer
Functions
query

Single-Row Multi-Row
Functions Functions

String Numeric Aggregate Window


Functions Functions Functions Functions
(Basics) (Advanced)
Date & Time NULL
Functions Functions

SQL Course | Date & Time Functions


SQL Course | Date & Time Functions
Date & Time
Runs outer query
Functions

Part
Runs outer Format
Runs &
outer Runs outer
Runs outer
Calculations Validation
Extraction
query Casting
query query
query

DAY FORMAT DATEADD ISDATE

MONTH CONVERT DATEDIFF

YEAR CAST

DATEPART

DATENAME

DATETRUNC
EOMONTH

SQL Course | Date & Time Funcitons


SQL Course | Date & Time Funcitons
PARTS
EXTRACTION

Baraa Khatib Salkini


YouTube | DATA WITH BARAA
SQL Course | Date & Time Funcitons
Date & Time
Runs outer query
Functions

Part
Runs outer Format
Runs &
outer Runs outer
Runs outer
Calculations Validation
Extraction
query Casting
query query
query

DAY FORMAT DATEADD ISDATE

MONTH CONVERT DATEDIFF

YEAR CAST

DATEPART

DATENAME

DATETRUNC
EOMONTH

SQL Course | Date & Time Funcitons


Quick Functions
YEAR, MONTH, DAY

SQL Course | Date & Time Funcitons


DATEPART

SQL Course | Date & Time Funcitons


DATENAME

SQL Course | Date & Time Funcitons


DATETRUNC

SQL Course | Date & Time Funcitons


DATETRUNC

SQL Course | Date & Time Funcitons


EOMONTH

SQL Course | Date & Time Funcitons


PART EXTRACTION
Syntax

DAY(date)
Runs outer query
DATEPART(part,
Runs outer query date)
MONTH(date)
Runs outer query
DATENAME(part,
Runs outer query date)
YEAR(date)
Runs outer query
DATETRUNC(part,
Runs outer query date)
EOMONTH(date)
Runs outer query

SQL Course | Date & Time Funcitons


DATA TYPES

SQL Course | Date & Time Funcitons


DATE PARTS

SQL Course | Date & Time Funcitons


How to Choose the Right Function?

SQL Course | Date & Time Funcitons


2025-08-20
09:38:54.840 Date Parts

INT String Datetime2


Part Abbre. DATEPART DATENMAME DATETRUNC
year yy, yyyy 2025 2025 2025-01-01 00:00:00

quarter qq,q 3 3 2025-07-01 00:00:00

month mm,m 8 August 2025-08-01 00:00:00


DATEPART dayofyear dy,y 232 232 2025-08-20 00:00:00

day dd, d 20 20 2025-08-20 00:00:00


DATENAME weekday dw 4 Wednesday Not supported
week wk,ww 34 34 2025-08-17 00:00:00

DATETUNC iso_week ns 34 34 2025-08-18 00:00:00

hour hh 9 9 2025-08-20 09:00:00


minute mi,n 45 45 2025-08-20 09:45:00

second ss,s 21 21 2025-08-20 09:45:21


millisecond ms 0 0 2025-08-20 09:45:21
microsecond msc 0 0 2025-08-20 09:45:21
nanosecond ns 0 0 Not supported
iso_week isowk, isoww 0 +00:00 Not supported
DATE
FORMATS

Baraa Khatib Salkini


YouTube | DATA WITH BARAA
SQL Course | Date & Time Funcitons
Date & Time
Runs outer query
Functions

Part
Runs outer Format
Runs &
outer Runs outer
Runs outer
Calculations Validation
Extraction
query Casting
query query
query

DAY FORMAT DATEADD ISDATE

MONTH CONVERT DATEDIFF

YEAR CAST

DATEPART

DATENAME

DATETRUNC
EOMONTH

SQL Course | Date & Time Funcitons


SQL Course | Date & Time Funcitons
SQL Course | Date & Time Funcitons
SQL Course | Date & Time Funcitons
SQL Course | Date & Time Funcitons
FORMAT
Syntax Syntax

FORMAT(value,
Runs outer query format [,culture])
Optional
Examples
FORMAT(OrderDate,'dd/MM/yyyy')
Runs outer query

FORMAT(OrderDate,'dd/MM/yyyy','ja-JP')
Runs outer query

FORMAT(1234.56,
Runs outer query 'D' ,'fr-FR')

Default Cultuer = ‘en-US’


SQL Course | Date & Time Funcitons
FORMAT
Use Case

SQL Course | Date & Time Funcitons


FORMAT
Use Case

SQL Course | Date & Time Funcitons


2025-08-20
Date & Time
18:55:45
Runs outer
Format query
Speicifiers

Format Description Result


D Full day name
d Day of the month 8/20/2025
dd Day of the month (two-digit) 20
ddd Abbreviated day name Wed
dddd Full day name Wednesday
M Month number 44044
MM Month number (two-digit) 8
FORMAT MMM Abbreviated month name Aug
MMMM Full month name August
yy Year (two-digit) 25
yyyy Year (four-digit) 2025
hh Hour (12-hour format, two-digit) 06
HH Hour (24-hour format, two-digit) 18
m Minutes August 20
mm Minutes (two-digit) 55
s Seconds 2025-08-20T18:55:45
ss Seconds (two-digit) 45
f Fractional seconds (one digit) Wednesday, August 20, 2025 6:55 PM
ff Fractional seconds (two digits) 00
fff Fractional seconds (three digits) 000
tt AM/PM designator PM

SQL Course | Date & Time Funcitons


2025-08-20
Number
18:55:45
Runs outer
Format query
Speicifiers

Format Description Query Result


N Numeric default SELECT FORMAT(1234.56, 'N') 1,234.56
P Percentage SELECT FORMAT(1234.56, 'P') 123,456.00 %
C Currency SELECT FORMAT(1234.56, 'C') $1,234.56
E Scientific notation SELECT FORMAT(1234.56, 'E') 1,23E+09
FORMAT F Fixed-point SELECT FORMAT(1234.56, 'F') 1234.56
N0 Numeric no decimals SELECT FORMAT(1234.56, 'N0') 1,235
N1 Numeric one decimal SELECT FORMAT(1234.56, 'N1') 1,234.6
N2 Numeric two decimals SELECT FORMAT(1234.56, 'N2') 1,234.56
N , de_DE Numeric (German) SELECT FORMAT(1234.56, 'N', 'de-DE') 1.234,56
N, en_US Numeric (US) SELECT FORMAT(1234.56, 'N', 'en-US') 1,234.56

SQL Course | Date & Time Funcitons


FORMAT
Use Case

SQL Course | Date & Time Funcitons


CONVERT

Syntax

CONVERT(data_type,
Runs outer query value [,style])

Optional
Examples
CONVERT(INT,
Runs outer query'124')

CONVERT(VARCHAR,
Runs outer query OrderDate, '34')

Default Style = 0

SQL Course | Date & Time Funcitons


SQL Course | Date & Time Funcitons
Date Time Datetime2
# Format Example # Format Example # Format Example
1 mm/dd/yy 12/30/25 8 hh:mm:ss 00:38:54 0 Mon dd yyyy hh:mm AM/PM Dec 30 2025 12:38AM
2 yy.mm.dd 25.12.30 14 hh:mm:ss:nnn 00:38:54:840 9 Mon dd yyyy hh:mm:ss:nnn AM/PM Dec 30 2025 12:38:54:840AM
3 dd/mm/yy 30/12/2025 24 hh:mm:ss 00:38:54 13 dd Mon yyyy hh:mm:ss:nnn AM/PM 30 Dec 2025 00:38:54:840AM
4 dd.mm.yy 30.12.25 108 hh:mm:ss 00:38:54 20 yyyy-mm-dd hh:mm:ss 2025-12-30 00:38:54
5 dd-mm-yy 30/12/2025 114 hh:mm:ss:nnn 00:38:54:840 21 yyyy-mm-dd hh:mm:ss:nnn 2025-12-30 00:38:54.840
6 dd-Mon-yy 30-Dec-25 22 mm/dd/yy hh:mm:ss AM/PM 12/30/25 12:38:54 AM
7 Mon dd, yy Dec 30, 25 25 yyyy-mm-dd hh:mm:ss:nnn 2025-12-30 00:38:54.840
10 mm-dd-yy 12-30-25 26 yyyy-dd-mm hh:mm:ss:nnn 2025-30-12 00:38:54.840
11 yy/mm/dd 25/12/1930 27 mm-dd-yyyy hh:mm:ss:nnn 12-30-2025 00:38:54.840
12 yymmdd 251230 Date
Runs&outer
Time 28
29
mm-yyyy-dd hh:mm:ss:nnn
dd-mm-yyyy hh:mm:ss:nnn
12-2025-30 00:38:54.840
30-12-2025 00:38:54.840
23 yyyy-mm-dd 30/12/2025
31 yyyy-dd-mm 2025-30-12 Styles
query 30 dd-yyyy-mm hh:mm:ss:nnn 30-2025-12 00:38:54.840
32 mm-dd-yyyy 12-30-2025 100 Mon dd yyyy hh:mm AM/PM Dec 30 2025 12:38AM
33 mm-yyyy-dd 12-2025-30 109 Mon dd yyyy hh:mm:ss:nnn AM/PM Dec 30 2025 12:38:54:840AM
34 dd-mm-yyyy 30/12/2025 CONVERT 113 dd Mon yyyy hh:mm:ss:nnn 30 Dec 2025 00:38:54:840
35 dd-yyyy-mm 30-2025-12 120 yyyy-mm-dd hh:mm:ss 2025-12-30 00:38:54
101 mm/dd/yyyy 12/30/2025 121 yyyy-mm-dd hh:mm:ss:nnn 2025-12-30 00:38:54.840
102 yyyy.mm.dd 2025.12.30 126 yyyy-mm-dd T hh:mm:ss:nnn 2025-12-30T00:38:54.840
103 dd/mm/yyyy 30/12/2025 127 yyyy-mm-dd T hh:mm:ss:nnn 2025-12-30T00:38:54.840
104 dd.mm.yyyy 30.12.2025
105 dd-mm-yyyy 30/12/2025 2025-08-20
106 dd Mon yyyy 30-Dec-25 18:55:45.840
107 Mon dd, yyyy Dec 30, 2025
110 mm-dd-yyyy 12-30-2025
111 yyyy/mm/dd 30/12/2025
112 yyyymmdd 20251230
CAST

Syntax

CAST(value AS data_type)
Runs outer query

Examples
CAST('123' AS INT)
Runs outer query

CAST('2025-08-20'
Runs outer query AS DATE)

No format can be specified

SQL Course | Date & Time Funcitons


SQL Course | Date & Time Funcitons
DATE
CALCULATIONS

Baraa Khatib Salkini


YouTube | DATA WITH BARAA
SQL Course | Date & Time Funcitons
Date & Time
Runs outer query
Functions

Part
Runs outer Format
Runs &
outer Runs outer
Runs outer
Calculations Validation
Extraction
query Casting
query query
query

DAY FORMAT DATEADD ISDATE

MONTH CONVERT DATEDIFF

YEAR CAST

DATEPART

DATENAME

DATETRUNC
EOMONTH

SQL Course | Date & Time Funcitons


SQL Course | Date & Time Funcitons
DATEADD

Syntax

DATEADD(part,
Runs outer query interval, date)

Examples
DATEADD(year,
Runs outer query 2, OrderDate)

DATEADD(month,
Runs outer query -4, OrderDate)

SQL Course | Date & Time Funcitons


DATEDIFF

SQL Course | Date & Time Funcitons


DATEDIFF

Syntax

DATEDIFF(part,
Runs outer query start_date, end_date)

Examples
DATEDIFF(year,
Runs outer query OrderDate, ShipDate)

DATEDIFF(day,
Runs outer query OrderDate, ShipDate)

SQL Course | Date & Time Funcitons


ISDATE

Syntax

ISDATE(value)
Runs outer query

ISDATE('2025-08-20')
Runs outer query

ISDATE(2025)
Runs outer query

SQL Course | Date & Time Funcitons


Date & Time
Runs outer query
Functions

Part
Runs outer Format
Runs &
outer Runs outer
Runs outer
Calculations Validation
Extraction
query Casting
query query
query

DAY FORMAT DATEADD ISDATE

MONTH CONVERT DATEDIFF

YEAR CAST

DATEPART

DATENAME

DATETRUNC
EOMONTH

SQL Course | Date & Time Funcitons


NULL
FUNCTIONS

Baraa Khatib Salkini


YouTube | DATA WITH BARAA
SQL Course | NULL Funcitons
SQL
Runs outer
Functions
query

Single-Row Multi-Row
Functions Functions

String Numeric Aggregate Window


Functions Functions Functions Functions
(Basics) (Advanced)
Date & Time NULL
Functions Functions

SQL Course | NULL Functions


What are NULLS?

SQL Course | NULL Functions


Where NULLS Come From?

SQL Course | NULL Functions


NULL FUNCTIONS

SQL Course | NULL Functions


SQL Course | NULL Functions
ISNULL

replaces NULL with the specified replacement value.

SQL Course | NULL Functions


ISNULL

replaces NULL with the specified replacement value.

SQL Course | NULL Functions


COALESCE

returns the first non-NULL value from the given expressions.

SQL Course | NULL Functions


COALESCE

returns the first non-NULL value from the given expressions.

SQL Course | NULL Functions


NULLIF

returns NULL if both values are equal; otherwise, it returns first value.

SQL Course | NULL Functions


IS NULL

check if a value is NULL.

SQL Course | NULL Functions


IS NULL

In SQL, use IS NULL instead of = NULL to correctly filter rows with NULL values.

SQL Course | NULL Functions


JOINS & IS NULL

SQL Course | NULL Functions


NULL vs Empty vs Blank

SQL Course | NULL Functions


CASE STATEMENT
CASE WHEN

Baraa Khatib Salkini


YouTube | DATA WITH BARAA
SQL Course | Case Statement
CASE STATEMENT
Syntax

The start of logic


Result, if the condition is true

CASE

Condition to be evaluated WHEN condition1 THEN result1

WHEN condition2 THEN result2

Default Value (Optional):



if none of the WHEN ELSE result
conditions are true

END

The End of logic

SQL Course | Case Statement


Use Case: Derive New Columns

The CASE statement in SQL categorizes values based on conditions

High
Sales

100
CASE
WHEN Sales >= 100 THEN 'High' 50
Medium
WHEN Sales >= 50 THEN 'Medium'
75
ELSE 'Low'
90
END
150
Low

SQL Course | Case Statement


Transformation & Standardization

The CASE statement in SQL is used for data transformation and


standardization by mapping specific values to standardized formats.

CASE
WHEN Country = 'Germany' THEN 'DE' CASE
WHEN Country = 'F' THEN 'Female'
WHEN Country = 'France' THEN 'FR'
WHEN Country = 'M' THEN 'Male'
WHEN Country = 'Italy' THEN 'IT'
ELSE 'n/a'
ELSE 'n/a'
END
END

Country CaseWhen Gender CaseWhen

Germany DE F Female

France FR M Male

Italy IT Null n/a

SQL Course | Case Statement


Column Name
to be evaluated (Only One) Column Value
To be compared

CASE CASE Country


WHEN Country = 'Germany' THEN 'DE' WHEN 'Germany' THEN 'DE'
WHEN Country = 'India' THEN 'IN' WHEN 'India' THEN 'IN'
WHEN Country = 'United States' THEN 'US' WHEN 'United States' THEN 'US'
WHEN Country = 'France' THEN 'FR' WHEN 'France' THEN 'FR'
WHEN Country = 'Italy' THEN 'IT' WHEN 'Italy' THEN 'IT'
ELSE 'n/a' ELSE 'n/a'

END END

Full Form Quick Form

SQL Course | Case Statement


SQL Course | Case Statement
SQL Course | Case Statement
SQL Course | Case Statement

You might also like