-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate_functions.sql
More file actions
84 lines (66 loc) · 2.08 KB
/
Copy pathdate_functions.sql
File metadata and controls
84 lines (66 loc) · 2.08 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
-- Get current date
SELECT CURRENT_DATE;
-- Get current date and time
SELECT NOW();
-- Extract year from hire date
SELECT employee_id, employee_name, YEAR(hire_date) AS hire_year
FROM Employees;
-- Extract month from hire date
SELECT employee_id, employee_name, MONTH(hire_date) AS hire_month
FROM Employees;
-- Extract day from hire date
SELECT employee_id, employee_name, DAY(hire_date) AS hire_day
FROM Employees;
-- Find employees hired in 2024
SELECT *
FROM Employees
WHERE YEAR(hire_date) = 2024;
-- Find orders placed in current month
SELECT *
FROM Orders
WHERE MONTH(order_date) = MONTH(CURRENT_DATE)
AND YEAR(order_date) = YEAR(CURRENT_DATE);
-- Find employees hired in the last 30 days
SELECT *
FROM Employees
WHERE hire_date >= CURRENT_DATE - INTERVAL 30 DAY;
-- Find orders placed in last 7 days
SELECT *
FROM Orders
WHERE order_date >= CURRENT_DATE - INTERVAL 7 DAY;
-- Add 10 days to current date
SELECT DATE_ADD(CURRENT_DATE, INTERVAL 10 DAY) AS future_date;
-- Subtract 15 days from current date
SELECT DATE_SUB(CURRENT_DATE, INTERVAL 15 DAY) AS past_date;
-- Find difference between two dates in days
SELECT employee_id,
employee_name,
DATEDIFF(CURRENT_DATE, hire_date) AS days_worked
FROM Employees;
-- Find age of employees based on birth date
SELECT employee_id,
employee_name,
TIMESTAMPDIFF(YEAR, birth_date, CURRENT_DATE) AS age
FROM Employees;
-- Find orders older than 90 days
SELECT *
FROM Orders
WHERE order_date < CURRENT_DATE - INTERVAL 90 DAY;
-- Format date as DD-MM-YYYY
SELECT employee_id,
employee_name,
DATE_FORMAT(hire_date, '%d-%m-%Y') AS formatted_hire_date
FROM Employees;
-- Find first day of current month
SELECT DATE_FORMAT(CURRENT_DATE, '%Y-%m-01') AS first_day_of_month;
-- Find last day of current month
SELECT LAST_DAY(CURRENT_DATE) AS last_day_of_month;
-- Find employees hired on weekend
SELECT *
FROM Employees
WHERE DAYOFWEEK(hire_date) IN (1, 7);
-- Find total months worked by employees
SELECT employee_id,
employee_name,
TIMESTAMPDIFF(MONTH, hire_date, CURRENT_DATE) AS months_worked
FROM Employees;