Repository navigation
Expand file tree
/
Copy pathTransformations.sql
More file actions
156 lines (109 loc) · 3 KB
/
Copy pathTransformations.sql
File metadata and controls
156 lines (109 loc) · 3 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use ecom_db;
-- 1. NUMERIC TRANSFORMATION : Numeric operatons are applied on columns are : discount %, Average, round, Addition,
-- Subtractions, Divison, Multiplication, Formulas etc.
SELECT
product_name,
unit_price AS real_price,
unit_price * 0.90 AS 'discount_price(10%)',
unit_price + 10 AS 'platform_fee(+10RS)',
ROUND(unit_price) AS 'rounded_price'
FROM
dim_product;
-- 2. DATE TRANSFORMATIONS :
-- 1. get current date
SELECT
-- current user timezone
now() AS "current_time",
-- current UTC date
UTC_DATE() "utc_date",
-- UTC Time HH:MM:SS
UTC_TIME() "utc_time",
-- FULL UTC TIME YYY-MM-DD HH:MM:SS
UTC_TIMESTAMP "utc_timestamp";
SELECT
date,
-- get year from date column
YEAR(date) AS 'year',
-- get month from date column
MONTH(date) AS 'month',
-- get day from date column
DAY(date) AS 'day',
-- get the day of the week : 1st day, 2nd day, 3rd day etc.
WEEKDAY(date) AS "x_day_of_week",
-- get the day name : Sunday, Monday ...
DAYNAME(date) AS 'day',
-- Add day in a date
ADDDATE(date,1) AS "date_+1_day",
-- Day gap between two dates
DATEDIFF(date,now()) AS "day_gap"
FROM
dim_date;
SELECT
-- DATE TYPECASTING : convert to full date timestamp
CAST('2005-12-01' AS DATETIME) AS 'casted_date';
-- TYPECASTING : Convert one data type to another data type
SELECT
course_id,
-- converting integer type cplumn to string(char/varchar)
CAST(course_id AS CHAR(10))
FROM
courses;
-- STRING FUNCTIONS :
-- 1. CONCAT (combine multiple columns)
SELECT
CONCAT(first_name,' ',last_name) AS 'full_name'
FROM
dim_customer;
-- 2. LENGTH : calculate length(total no. of characters in a column)
SELECT
*,
-- Calculate number of characters in first_name
LENGTH(first_name) AS 'first_name_length'
FROM
dim_customer;
-- 3. LOWER/UPPER : convert string values to upper and lower case
SELECT
UPPER(first_name),
LOWER(last_name)
FROM
dim_customer;
-- 4. SUBSTRING : Slice the strings
SELECT
-- get only 3 characters from starting
SUBSTRING(first_name,1,3)
FROM
dim_customer;
-- 5. REPLACE : replace an existing value
SELECT
-- replace @ character in email column with : at
REPLACE(email,'@','at')
FROM
dim_customer;
-- 6. LEFT/RIGHT : get the number of charters from LEFT or RIGHT
SELECT
first_name,
last_name,
-- get 3 characters from RIGHT
LEFT(first_name,3),
-- get 2 characters from LEFT
RIGHT(last_name,2)
FROM
dim_customer;
-- 7. REVERSE : to reverse a given sequence of characters.
SELECT
first_name,
REVERSE(first_name)
FROM
dim_customer;
-- 8. REPEAT : Repeat a value in column
SELECT
first_name,
-- REPEAT first_name 3 times
REPEAT(first_name,3)
FROM
dim_customer;
-- 9. CONCATE_WS() : used to concat the multiple columns when delimeter is same
SELECT
CONCAT_WS(' ', first_name, last_name, country)
FROM
dim_customer;