-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopic_3_functions.sql
More file actions
142 lines (90 loc) · 2.36 KB
/
topic_3_functions.sql
File metadata and controls
142 lines (90 loc) · 2.36 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
## mysql numeric functions
select round(5.73, 1);
select truncate(5.1234, 2);
select ceiling(1.234);
select floor(1.234);
select abs(-2);
select rand(); # random value between 0 and 1.
## strings:
select length('abc');
select upper('abc');
select lower('ABC');
select ltrim(' abc');
select rtrim('abcd ');
select left('abcdef', 2);
select right('abcdef', 3);
select substring('abcdef', 2, 3); # start position, length
select substring('abcdef', 2); # start position, length
select locate('e', 'abcdef');
select locate('e', 'abcdEf');
select locate('q', 'abcdef');
select locate('def', 'abcdef');
select replace('abc', 'c', 'C');
select concat('abc', 'def');
use sql_store;
select concat(first_name, ' ', last_name) as full_name
from customers
;
select *
from customers
;
## date and time: mysql date format strings
select now();
select curtime();
select curdate();
select year(now());
select day(now());
select hour(now());
select minute(now());
select second(now());
select dayname(now());
select monthname(now());
select extract(year from now());
select *
from orders
where year(order_date) = year(now());
select date_format(now(), '%y');
select date_format(now(), '%M %Y');
select date_format(now(), '%M %d %Y');
select time_format(now(), '%H:%i %p');
select date_add(now(), interval 1 day);
select date_add(now(), interval 1 year);
select date_add(now(), interval -1 day);
select date_sub(now(), interval 1 year);
select datediff('2021-1-10', '2020-10-10');
select time_to_sec('09:40') - time_to_sec('09:20');
select ifnull(null, 'xxx');
select coalesce(null, null, 'xxx', 'xx');
select
concat(first_name, ' ', last_name)
, ifnull(phone, 'not known') as phone
, coalesce(phone, 'not known') as phone
from
customers;
## if clause:
select
order_id
, order_date
, if (year(order_date) = year(now()),
'active',
'inactive') as category
from orders;
select
product_id
, name
, count(*) as orders
, if(count(*) > 1, 'many times', 'once') as frequency
from
products
join order_items using (product_id)
group by product_id, name;
## case clause
select
order_id
, case
when year(order_date) = year(now()) then 'active'
when year(order_date) = year(now()) - 1 then 'last year'
when year(order_date) < year(now()) - 1 then 'archived'
else 'future'
end as category
from orders;