-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.Date transformations.sql
More file actions
53 lines (42 loc) · 1.1 KB
/
Copy path8.Date transformations.sql
File metadata and controls
53 lines (42 loc) · 1.1 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
use ecom;
-- DATE TRANSFORMATIONS (Very Important)
select * from dim_date;
-- Functions used to fetch current date
select
date,
now() as 'current_timestamp', -- timestamp according to our timezone
utc_date() as 'current_date', -- use single quotes as it is a reserved keyword
utc_time() as 'current_utc_time',
utc_timestamp() as 'utc_timestamp'
from
dim_date;
-- Extract components from date
select
date,
year(date),
month(date),
day(date),
week(date),
weekday(date),
dayname(date),
date(utc_timestamp()), -- Extract date from timestamp
adddate(date,2), -- Add 2 days from date
subdate(date,2) -- Subtract 2 days from date
from
dim_date;
-- Application 1 : Find orders placed before 30 days
select
datediff(date(utc_timestamp),date) as total_days
from
dim_date;
-- Casting string to date
select
cast('25-01-01' as datetime) -- cast is used to convert any type to another (type casting)
from
dim_date;
-- Convert date formats
select
date,
date_format(date,'%W %M %e %Y') as converted_date -- There are lot of such patterns (
from
dim_date;