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 |
+--------------------------------------------------------------+
Related
I've been trying to solve this problem for a few hours , so far what i have reached is something like this:
SELECT * FROM mytable WHERE DATE(datetime_column)
BETWEEN DATE_SUB(DATE_FORMAT(NOW(),'%Y-%M-01'),INTERVAL 1 DAY) AND NOW()
AND HOUR(datetime_column) > = '13'
ORDER BY
datetime_column
Besides the day interval, i also need the hour interval which must be between 13h pm from last day of the last month till midday for present date.
The fiddle doesnt seem to work with the current query but its working in my database, but check fiddle for more info:
https://www.db-fiddle.com/f/a3jui7aPMevU81Cka4BqXg/0
And sample data:
CREATE TABLE mytable (
id INT,
datetime_column DATETIME
);
INSERT INTO mytable (id,datetime_column) VALUES
(1,'2020-03-31 08:00:00'),
(2,'2020-03-31 13:00:00'),
(3,'2020-04-02 14:30:00'),
(4,'2020-04-06 18:00:00'),
(5,'2020-04-21 05:00:00'),
(6,'2020-04-23 13:00:00'),
(7,'2020-04-23 14:00:00');
If more info is necessary please let me know.
I think this what you want
SELECT * FROM mytable
WHERE (
DATE(datetime_column) > LAST_DAY(CURDATE() - INTERVAL 1 MONTH)
AND datetime_column < (CURDATE() + INTERVAL 12 HOUR)
)
OR (
DATE(datetime_column) = LAST_DAY(CURDATE() - INTERVAL 1 MONTH)
AND HOUR(datetime_column) >= 13
);
+------+---------------------+
| id | datetime_column |
+------+---------------------+
| 2 | 2020-03-31 13:00:00 |
| 3 | 2020-04-02 14:30:00 |
| 4 | 2020-04-06 18:00:00 |
| 5 | 2020-04-21 05:00:00 |
+------+---------------------+
2 rows in set (0.00 sec)
Here LAST_DAY(CURDATE() - INTERVAL 1 MONTH) gives the last day of previous month.
And datetime_column < (CURDATE() + INTERVAL 12 HOUR) tests if that time is before mid day of today.
I have these rows
id | start_time |
1 | 2018-06-15 02:00:00 |
2 | 2018-06-15 02:45:00 |
3 | 2018-06-15 03:45:00 |
I want to select rows that are 1 hour before the start_time. So if the time is 2018-06-15 01:00:00 then the first row should be returned.
How do i do this? I've tried below but i don't know how to subtract 1 hour from start_time.
SELECT *
FROM table1
WHERE DATE_FORMAT(start_time, '%Y-%m-%d %H') <= DATE_FORMAT(NOW(), '%Y-%m-%d %H');
To subtract hours ,use date_sub function
In your case
SELECT DATE_SUB(DATE_FORMAT(start_time, '%Y-%m-%d %H'), INTERVAL 1 HOUR)
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
I have this kind of table with time based data:
| entity_id | ttime | value |
-------------------------------------------
| 1 | 2014-11-01 00:00:04 | 553 |
| 1 | ... | 600 |
| 2 | ... | 234 |
I want to get the average of the value grouped by week and entity_id. But I would like also the starting day of the week to appear in the results. Additional complexity is that the week starts on wednesday.
I can group by YEAR(ttime + INTERVAL 3 DAY), WEEK(ttime + INTERVAL 3 DAY) but is it possible to print the first day of the group (wednesday) in the results?
Thanks
maybe something like this:
SELECT
`entity_id`,
DATE_SUB(ttime, INTERVAL WEEKDAY(ttime)-2 DAY),
SUM(`value`)
FROM `table`
GROUP BY `entity_id`, YEARWEEK(ttime + INTERVAL 4 DAY)
SqlFiddle
I found this solution:
SELECT
str_to_date(CONCAT(YEAR(ttime + INTERVAL -3 DAY),
WEEK(ttime + INTERVAL -3 DAY), 'Wednesday'), '%X%V %W') as WeekCommencing,
entity_id, AVG(value),
FROM `table`
GROUP BY WeekCommencing, entity_id
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 |
+---------------------+---------------------+