-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lesson 4. Date and Time Functions.sql
45 lines (37 loc) · 1.41 KB
/
Lesson 4. Date and Time Functions.sql
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
/****************************************************************************
************************ T R A N S A C T - S Q L ************************
*****************************************************************************
***** Lesson IV ****** Scalar Functions ************************
****************************************************************************/
--5. Date and Time Functions
----CURRENT_TIMESTAMP è GETDATE
SELECT CURRENT_TIMESTAMP,
GETDATE()
USE ITVDNdb
GO
--DATENAME, DATEPART, DAY, MONTH, YEAR
--Íàéòè ñîòðóäíèêîâ, êîòîðûå ðàäèëèñü â äåêàáðå
SELECT Id, LName, BirthDate
FROM Employees
WHERE DATEPART(MONTH, BirthDate) = 12
DECLARE @today date = GETDATE()
SELECT DATENAME(yy, @today),
DATENAME(MONTH, @today),
DATEPART(MONTH, @today),
DATEPART(QUARTER, @today),
DAY(@today) [DAY],
MONTH(@today) [MONTH],
YEAR(@today) [YEAR]
--DATEFROMPARTS, DATETIMEFROMPARTS, TIMEFROMPARTS
SELECT DATEFROMPARTS(2017, 05, 17),
DATETIMEFROMPARTS(2017, 05, 17, 04, 30, 12, 123),
TIMEFROMPARTS(04, 30, 12, 1234567, 7)
SELECT DATEDIFF(MONTH, '20160901', '20161201'),
DATEDIFF(MONTH, '20160831', '20161201'),
DATEADD(MONTH, -3, GETDATE())
SELECT Id, LName, BirthDate
FROM Employees
WHERE DATEDIFF(YEAR, BirthDate, GETDATE()) < 30
SELECT Id, LName, BirthDate
FROM Employees
WHERE BirthDate > DATEADD(YEAR,-30, GETDATE())