Finding Dates for Days of the Current Week : DATE_ADD « Date Time « SQL / MySQL






Finding Dates for Days of the Current Week

     
mysql>
mysql> CREATE TABLE date_val
    -> (
    ->  d       DATE
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO date_val (d) VALUES('1864-02-28');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO date_val (d) VALUES('1900-01-15');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO date_val (d) VALUES('1987-03-05');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO date_val (d) VALUES('1999-12-31');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO date_val (d) VALUES('2000-06-04');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql>
mysql> SELECT d, DAYNAME(d) AS day,
    -> DATE_ADD(d,INTERVAL 1-DAYOFWEEK(d) DAY) AS Sunday,
    -> DATE_ADD(d,INTERVAL 7-DAYOFWEEK(d) DAY) AS Saturday
    -> FROM date_val;
+------------+----------+------------+------------+
| d          | day      | Sunday     | Saturday   |
+------------+----------+------------+------------+
| 1864-02-28 | Sunday   | 1864-02-28 | 1864-03-05 |
| 1900-01-15 | Monday   | 1900-01-14 | 1900-01-20 |
| 1987-03-05 | Thursday | 1987-03-01 | 1987-03-07 |
| 1999-12-31 | Friday   | 1999-12-26 | 2000-01-01 |
| 2000-06-04 | Sunday   | 2000-06-04 | 2000-06-10 |
+------------+----------+------------+------------+
5 rows in set (0.00 sec)

mysql>
mysql> drop table date_val;
Query OK, 0 rows affected (0.00 sec)

   
    
    
    
    
  








Related examples in the same category

1.what time will it be in 60 hours?
2.For DATETIME- or TIMESTAMP-formatted strings, you can use DATE_ADD( ) to introduce a temporal context:
3.Finding Dates for Weekdays of Other Weeks
4.WHERE DATE_ADD(d,INTERVAL 6 MONTH) >= CURDATE( );
5.Adding a Temporal Interval to a Date
6.Shifting Dates by a Known Amount
7.Adding 3 days and 4 hours produces this result
8.Perform the week shift first