MySQL: DATE_ADD - mysql

Is there a difference between:
SELECT DATE_ADD('2005-01-01', INTERVAL 3 MONTH);
and
SELECT '2005-01-01' + INTERVAL 3 MONTH;

No, they're the same.

I asked a similar question just now and found the answer myself. Here's the justification why they are the same:
SELECT BENCHMARK(20000000, DATE_ADD(NOW(), INTERVAL 3 MONTH));
+--------------------------------------------------------+
| BENCHMARK(20000000, DATE_ADD(NOW(), INTERVAL 3 MONTH)) |
+--------------------------------------------------------+
| 0 |
+--------------------------------------------------------+
1 row in set (1.70 sec)
SELECT BENCHMARK(20000000, NOW() + INTERVAL 3 MONTH);
+-----------------------------------------------+
| BENCHMARK(20000000, NOW() + INTERVAL 3 MONTH) |
+-----------------------------------------------+
| 0 |
+-----------------------------------------------+
1 row in set (1.71 sec)

Related

How to add one year and two days to a date in mysql

I want to add one year and two days to a date in mysql
I'm tring this code, but it is not working.
Please help me. Thank you.
DATE_ADD(vehicle_master.expire_date, INTERVAL 1 YEAR +2 DAY) AS expire_date,
Use twice date_add instead:
mysql> select DATE_ADD(DATE_ADD('2016-04-01', INTERVAL 1 YEAR), INTERVAL 2 DAY);
+-------------------------------------------------------------------+
| DATE_ADD(DATE_ADD('2016-04-01', INTERVAL 1 YEAR), INTERVAL 2 DAY) |
+-------------------------------------------------------------------+
| 2017-04-03 |
+-------------------------------------------------------------------+
1 row in set (0.00 sec)
You can't combine intervals in that way.
To do this, you would:
DATE_ADD(DATE_ADD(vehicle_master.expire_date, INTERVAL 1 YEAR), 2 DAY) as somedate

finding a specific date in MySql

I knew this will give syntax error.
SELECT DATE_SUB(DATE_SUB(now(),INTERVAL 3 MONTH)),INTERVAL EXTRACT(DAY FROM (DATE_SUB(now(),INTERVAL 3 MONTH))) DAY) as oldDate
I am new to SQL and in here I am trying to find 3 month old date and the first day of that month.
for example 3 month old date from today will be 28-11-2014 so I have to show 01-11-2014.
so how to sort above code to avoid syntax error.
You can use DATE_FORMAT to format the date according to your requirements.
SELECT DATE_SUB(NOW(), INTERVAL 3 MONTH) 3months_ago,
DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 3 MONTH), "01-%m-%Y") first_day
FROM DUAL
+---------------------+------------+
| 3months_ago | first_day |
+---------------------+------------+
| 2014-11-28 09:20:50 | 01-11-2014 |
+---------------------+------------+
You can use date_format() function along with date_sub() to get the 1st day of month
mysql> select date_format(date_sub(curdate(),interval 3 month),'01-%m-%Y') ;
+--------------------------------------------------------------+
| date_format(date_sub(curdate(),interval 3 month),'01-%m-%Y') |
+--------------------------------------------------------------+
| 01-11-2014 |
+--------------------------------------------------------------+

mysql query to select between previous and next month

Trying to select all days from the start of the previous month to the end of the next month:
USE test;
SELECT * FROM MyTable
WHERE col_date BETWEEN DATE_ADD(DATE_ADD(NOW(), INTERVAL -1 MONTH))
AND DATE_ADD(DATE_ADD(NOW(), INTERVAL +1 MONTH))
Its saying my syntax in incorrect, but I saw snippet of the DATE_ADD function being used like this.
You can use date_sub()
date_sub(now(), INTERVAL 1 MONTH)
So the query should be something as
USE test;
SELECT * FROM MyTable
WHERE
col_date BETWEEN DATE_SUB(NOW(), INTERVAL 1 MONTH) AND DATE_ADD(NOW(), INTERVAL 1 MONTH)
Here how it looks like in mysql
mysql> select date_sub(now(), INTERVAL 1 MONTH) as previous_month , date_add(now(),INTERVAL 1 MONTH) as next_month;
+---------------------+---------------------+
| previous_month | next_month |
+---------------------+---------------------+
| 2014-05-06 22:27:35 | 2014-07-06 22:27:35 |
+---------------------+---------------------+
1 row in set (0.01 sec)
mysql> select NOW() - INTERVAL 1 MONTH as previous_month ,NOW() + INTERVAL 1 MONTH as next_month ;
+---------------------+---------------------+
| previous_month | next_month |
+---------------------+---------------------+
| 2014-05-06 22:28:39 | 2014-07-06 22:28:39 |
+---------------------+---------------------+

MySQL select data from last week

I was wondering how to select all the data from last week? Between Monday to Friday, assuming that today is a Monday. I actually don't have a datetime format column.
Here's what I tried so far:
SELECT * FROM logs
WHERE WEEKDAY(CONCAT(year,'-',month,'-',day)) BETWEEN 0 AND 4
AND YEARWEEK( date_added2 ) = YEARWEEK( CURRENT_DATE( ) - INTERVAL 7 DAY) AND deleted='n';
And this is my table, table logs:
Could you try this? you can test here http://www.sqlfiddle.com/#!2/3989b1/3/0
SELECT *
FROM logs,
(SELECT (CURRENT_DATE() - INTERVAL (WEEKDAY(CURRENT_DATE()) + 1 ) % 7 DAY) AS sun_day) t1
WHERE
logs.date_added2 BETWEEN t1.sun_day - INTERVAL 6 DAY
AND t1.sun_day - INTERVAL 2 DAY;
How it works
folling query returns sunday of week
SELECT '2014-01-09' - INTERVAL (WEEKDAY('2014-01-09') + 1 ) % 7 DAY;
+--------------------------------------------------------------+
| '2014-01-09' - INTERVAL (WEEKDAY('2014-01-09') + 1 ) % 7 DAY |
+--------------------------------------------------------------+
| 2014-01-05 |
+--------------------------------------------------------------+
SELECT '2014-01-10' - INTERVAL (WEEKDAY('2014-01-10') + 1 ) % 7 DAY;
+--------------------------------------------------------------+
| '2014-01-10' - INTERVAL (WEEKDAY('2014-01-10') + 1 ) % 7 DAY |
+--------------------------------------------------------------+
| 2014-01-05 |
+--------------------------------------------------------------+
so, t1.sun_day - INTERVAL 6 holds monday of previous week, and t1.sun_day - INTERVAL 2 DAY for friday of previous week.
mysql> SELECT #sunday := (CURRENT_DATE() - INTERVAL (WEEKDAY(CURRENT_DATE()) + 1 ) % 7 DAY) AS sunday;
+------------+
| sunday |
+------------+
| 2014-01-05 |
+------------+
mysql> SELECT #sunday - INTERVAL 6 DAY, #sunday - INTERVAL 2 DAY;
+--------------------------+---------------------------+
| #sunday - INTERVAL 6 DAY | #sunday - INTERVAL 2 DAY |
+--------------------------+---------------------------+
| 2013-12-30 | 2014-01-03 |
+--------------------------+---------------------------+
So, logs.date_added2 BETWEEN t1.sun_day - INTERVAL 6 DAY AND t1.sun_day - INTERVAL 2 DAY would find logs from monday to friday.

Mysql total elapsed times from two dates

If i have two rows
id created start stop
1 28-01-2011 23:00 28-01-2011 23:00 28-01-2011 23:01
2 28-01-2011 23:10 28-01-2011 23:10 28-01-2011 23:11
What query can i run to get the total elapsed time for one date, so for 28-01-2011 the total time was 2 minutes.
Is this possible?
well one option is straight forward:
select sum(unix_timestamp(stop) -
unix_timestamp(start))/60 from table
where date(created) = '28-01-2011';
mysql> select now(), date_add(now(), interval 10 minute);
+---------------------+-------------------------------------+
| now() | date_add(now(), interval 10 minute) |
+---------------------+-------------------------------------+
| 2011-10-04 13:29:56 | 2011-10-04 13:39:56 |
+---------------------+-------------------------------------+
1 row in set (0.01 sec)
mysql> select sum(unix_timestamp(now()) -
unix_timestamp(date_add(now(), interval 10 minute)));
+----------------------------------------------------------------------------------+
| sum(unix_timestamp(now()) - unix_timestamp(date_add(now(), interval 10 minute))) |
+----------------------------------------------------------------------------------+
| -600 |
+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select sum(unix_timestamp(now()) -
unix_timestamp(date_add(now(), interval 10 minute))) / 60 ;
+---------------------------------------------------------------------------------------+
| sum(unix_timestamp(now()) - unix_timestamp(date_add(now(), interval 10 minute))) / 60 |
+---------------------------------------------------------------------------------------+
| -10.0000 |
+---------------------------------------------------------------------------------------+
1 row in set (0.00 sec)