-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_06-DATE, CONVERT.sql
66 lines (53 loc) · 1.76 KB
/
02_06-DATE, CONVERT.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
SELECT 'GETDATE' AS STFunction, GETDATE();
SELECT 'SYSDATETIME' AS STFunction, SYSDATETIME();
GO
-- MONTH, DAY, YEAR
SELECT DISTINCT o.OrderDate,
MONTH(o.OrderDate) AS OrderMonth,
DAY(o.OrderDate) AS OrderDay,
YEAR(o.OrderDate) AS OrderYear
FROM Sales.Orders AS o
-- DATEPART ( datepart , date )
SELECT o.OrderID,
o.OrderDate,
DATEPART(m, o.OrderDate) AS OrderMonth,
DATEPART(d, o.OrderDate) AS OrderDay,
DATEPART(yy, o.OrderDate) AS OrderYear
FROM Sales.Orders AS o
-- -----------------------------
-- DATEDIFF ( datepart , startdate , enddate )
-- Years
SELECT DATEDIFF (yy,'2007-01-01', '2008-01-01') AS 'YearDiff';
-- Days
SELECT DATEDIFF (dd,'2007-01-01', '2008-01-01') AS 'DayDiff';
-- Months
SELECT o.OrderID,
o.OrderDate,
o.PickingCompletedWhen,
DATEDIFF(mm, o.OrderDate, o.PickingCompletedWhen) AS MonthsDiff
FROM Sales.Orders o
WHERE DATEDIFF(mm, o.OrderDate, o.PickingCompletedWhen) > 0
-- DATEADD (datepart , number , date )
SELECT o.OrderID,
o.OrderDate,
DATEADD (yy, 1, o.OrderDate) AS DateAddOneYear,
EOMONTH(o.OrderDate) AS EndOfMonth
FROM Sales.Orders o
-- BETWEEN date
SELECT PickingCompletedWhen, *
FROM Sales.Orders o
WHERE PickingCompletedWhen BETWEEN '2013-01-05' AND
-- DATETIME to date, CONVERT
-- Показать заказы с 2013-01-05 по 2013-01-07 включительно.
-- Есть ошибка?
SELECT
PickingCompletedWhen,
cast(PickingCompletedWhen as date),
convert(nvarchar(16), PickingCompletedWhen, 104) as DateAsString,
-- floor(cast(PickingCompletedWhen as float)) as PickingDateWithoutTime,
*
FROM Sales.Orders o
WHERE PickingCompletedWhen BETWEEN '2013-01-05' AND '2013-01-07'
-- COLLATION
SELECT name, description
FROM fn_helpcollations()