SQL Server/T-SQL Tutorial/Date Functions/DATEADD
Версия от 13:46, 26 мая 2010; (обсуждение)
Содержание
- 1 Calculating the First Day of the Month
- 2 DATEADD adds a time interval to a date in order to arrive at the new date: DATEADD(datepart,number,date)
- 3 DATEADD adds or subtract a number of days, months, or years from a specific date.
- 4 Determine the date 90 days after April
- 5 Finding the Last Day of the Month
- 6 select DATEADD(day, 1, "2002-35:00")
- 7 SELECT DATEADD(day, 14, GETDATE())
- 8 select DATEADD(hour, 1, "2002-35:00")
- 9 select DATEADD(minute, 1, "2002-35:00")
- 10 select DATEADD(month, 1, "2002-35:00")
- 11 select DATEADD(month, -1, "2002-35:00") (Minus)
- 12 select DATEADD(quarter, 1, "2002-35:00")
- 13 select DATEADD(second, 1, "2002-35:00")
- 14 select DATEADD(week, 1, "2002-35:00")
- 15 select DATEADD(year, 1, "2002-35:00")
- 16 select DATEADD(year, 1.5, "2002-35:00")
Calculating the First Day of the Month
37>
38> CREATE TABLE employee(
39> id INTEGER NOT NULL PRIMARY KEY,
40> first_name VARCHAR(10),
41> last_name VARCHAR(10),
42> salary DECIMAL(10,2),
43> start_Date DATETIME,
44> region VARCHAR(10),
45> city VARCHAR(20),
46> managerid INTEGER
47> );
48> GO
1> INSERT INTO employee VALUES (1, "Jason" , "Martin", 5890,"2005-03-22","North","Vancouver",3);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (2, "Alison", "Mathews",4789,"2003-07-21","South","Utown",4);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (3, "James" , "Smith", 6678,"2001-12-01","North","Paris",5);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (4, "Celia" , "Rice", 5567,"2006-03-03","South","London",6);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (5, "Robert", "Black", 4467,"2004-07-02","East","Newton",7);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (6, "Linda" , "Green" , 6456,"2002-05-19","East","Calgary",8);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (7, "David" , "Larry", 5345,"2008-03-18","West","New York",9);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (8, "James" , "Cat", 4234,"2007-07-17","West","Regina",9);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (9, "Joan" , "Act", 6123,"2001-04-16","North","Toronto",10);
2> GO
(1 rows affected)
1>
2> select * from employee;
3> GO
id first_name last_name salary start_Date region city managerid
----------- ---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
1 Jason Martin 5890.00 2005-03-22 00:00:00.000 North Vancouver 3
2 Alison Mathews 4789.00 2003-07-21 00:00:00.000 South Utown 4
3 James Smith 6678.00 2001-12-01 00:00:00.000 North Paris 5
4 Celia Rice 5567.00 2006-03-03 00:00:00.000 South London 6
5 Robert Black 4467.00 2004-07-02 00:00:00.000 East Newton 7
6 Linda Green 6456.00 2002-05-19 00:00:00.000 East Calgary 8
7 David Larry 5345.00 2008-03-18 00:00:00.000 West New York 9
8 James Cat 4234.00 2007-07-17 00:00:00.000 West Regina 9
9 Joan Act 6123.00 2001-04-16 00:00:00.000 North Toronto 10
(9 rows affected)
1>
2>
3>
4> SELECT
5> CONVERT (char (6), Start_Date, 112) + "01"
6> FROM
7> Employee
8>
9> GO
--------
20050301
20030701
20011201
20060301
20040701
20020501
20080301
20070701
20010401
(9 rows affected)
1>
2>
3> drop table employee;
4> GO
DATEADD adds a time interval to a date in order to arrive at the new date: DATEADD(datepart,number,date)
Parts of the date.
Date Part Description Value Range
YY Year 1753 to 9999
QQ Quarter 1 to 4
MM Month 1 to 12
DY Day of year 1 to 366
DD Day of month 1 to 31
WK Week of year 1 to 53
DW Weekday 1 to 7 or Sun. to Sat.
HH Hour 0 to 23 (military time)
MI Minute 0 to 59
SS Second 0 to 59
MS Millisecond 0 to 999
34> SELECT DATEADD(DY,30,"10/01/97")
35> GO
-----------------------
1997-10-31 00:00:00.000
(1 rows affected)
1>
DATEADD adds or subtract a number of days, months, or years from a specific date.
To subtract days, use a negative number in the second parameter
9> SELECT DATEADD(dd,12,"24 March 1964")
10> GO
-----------------------
1964-04-05 00:00:00.000
(1 rows affected)
The syntax for DATEADD requires 3 parameters.
DATEADD(what_to_add,number_to_add,date_to_add_it_to)
The first parameter can define what you are wishing to add to the date.
Determine the date 90 days after April
6>
7> SELECT DATEADD(Day, 90, "4-29-1988")
8> GO
-----------------------
1988-07-28 00:00:00.000
(1 rows affected)
1>
Finding the Last Day of the Month
3>
4> CREATE TABLE employee(
5> id INTEGER NOT NULL PRIMARY KEY,
6> first_name VARCHAR(10),
7> last_name VARCHAR(10),
8> salary DECIMAL(10,2),
9> start_Date DATETIME,
10> region VARCHAR(10),
11> city VARCHAR(20),
12> managerid INTEGER
13> );
14> GO
1> INSERT INTO employee VALUES (1, "Jason" , "Martin", 5890,"2005-03-22","North","Vancouver",3);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (2, "Alison", "Mathews",4789,"2003-07-21","South","Utown",4);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (3, "James" , "Smith", 6678,"2001-12-01","North","Paris",5);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (4, "Celia" , "Rice", 5567,"2006-03-03","South","London",6);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (5, "Robert", "Black", 4467,"2004-07-02","East","Newton",7);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (6, "Linda" , "Green" , 6456,"2002-05-19","East","Calgary",8);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (7, "David" , "Larry", 5345,"2008-03-18","West","New York",9);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (8, "James" , "Cat", 4234,"2007-07-17","West","Regina",9);
2> GO
(1 rows affected)
1> INSERT INTO employee VALUES (9, "Joan" , "Act", 6123,"2001-04-16","North","Toronto",10);
2> GO
(1 rows affected)
1>
2> select * from employee;
3> GO
id first_name last_name salary start_Date region city managerid
----------- ---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
1 Jason Martin 5890.00 2005-03-22 00:00:00.000 North Vancouver 3
2 Alison Mathews 4789.00 2003-07-21 00:00:00.000 South Utown 4
3 James Smith 6678.00 2001-12-01 00:00:00.000 North Paris 5
4 Celia Rice 5567.00 2006-03-03 00:00:00.000 South London 6
5 Robert Black 4467.00 2004-07-02 00:00:00.000 East Newton 7
6 Linda Green 6456.00 2002-05-19 00:00:00.000 East Calgary 8
7 David Larry 5345.00 2008-03-18 00:00:00.000 West New York 9
8 James Cat 4234.00 2007-07-17 00:00:00.000 West Regina 9
9 Joan Act 6123.00 2001-04-16 00:00:00.000 North Toronto 10
(9 rows affected)
1>
2>
3>
4>
5> SELECT
6> DATEADD (dd, -1,
7> DATEADD (mm, 1,
8> CONVERT (char (6), Start_Date, 112) + "01"))
9> FROM
10> Employee
11> GO
-----------------------
2005-03-31 00:00:00.000
2003-07-31 00:00:00.000
2001-12-31 00:00:00.000
2006-03-31 00:00:00.000
2004-07-31 00:00:00.000
2002-05-31 00:00:00.000
2008-03-31 00:00:00.000
2007-07-31 00:00:00.000
2001-04-30 00:00:00.000
(9 rows affected)
1>
2>
3> drop table employee;
4> GO
select DATEADD(day, 1, "2002-35:00")
3> select DATEADD(day, 1, "2002-09-30 11:35:00")
4> GO
-----------------------
2002-10-01 11:35:00.000
(1 rows affected)
SELECT DATEADD(day, 14, GETDATE())
3> SELECT DATEADD(day, 14, GETDATE())
4> GO
-----------------------
2008-08-31 13:20:40.540
(1 rows affected)
1>
select DATEADD(hour, 1, "2002-35:00")
2>
3> select DATEADD(hour, 1, "2002-09-30 11:35:00")
4> GO
-----------------------
2002-09-30 12:35:00.000
(1 rows affected)
select DATEADD(minute, 1, "2002-35:00")
2>
3>
4> select DATEADD(minute, 1, "2002-09-30 11:35:00")
5> GO
-----------------------
2002-09-30 11:36:00.000
(1 rows affected)
select DATEADD(month, 1, "2002-35:00")
2>
3>
4> select DATEADD(month, 1, "2002-09-30 11:35:00")
5> GO
-----------------------
2002-10-30 11:35:00.000
(1 rows affected)
select DATEADD(month, -1, "2002-35:00") (Minus)
2>
3> select DATEADD(month, -1, "2002-09-30 11:35:00")
4> GO
-----------------------
2002-08-30 11:35:00.000
(1 rows affected)
select DATEADD(quarter, 1, "2002-35:00")
2>
3> select DATEADD(quarter, 1, "2002-09-30 11:35:00")
4> GO
-----------------------
2002-12-30 11:35:00.000
(1 rows affected)
select DATEADD(second, 1, "2002-35:00")
2>
3> select DATEADD(second, 1, "2002-09-30 11:35:00")
4> GO
-----------------------
2002-09-30 11:35:01.000
(1 rows affected)
select DATEADD(week, 1, "2002-35:00")
2>
3> select DATEADD(week, 1, "2002-09-30 11:35:00")
4> GO
-----------------------
2002-10-07 11:35:00.000
(1 rows affected)
select DATEADD(year, 1, "2002-35:00")
2>
3> select DATEADD(year, 1, "2002-09-30 11:35:00")
4> GO
-----------------------
2003-09-30 11:35:00.000
(1 rows affected)
select DATEADD(year, 1.5, "2002-35:00")
2>
3> select DATEADD(year, 1.5, "2002-09-30 11:35:00")
4> GO
-----------------------
2003-09-30 11:35:00.000
(1 rows affected)