I am using date_add function to add 12 hours and 30 minutes in current date.
select DATE_ADD( CURDATE( ) , INTERVAL '12:30' HOUR_MINUTE )
gives me 2016-01-03 12:30:00
I just need date after adding 12:30.
what is the correct way to do same?
Just use the date function:
select DATE(DATE_ADD( CURDATE( ) , INTERVAL '12:30' HOUR_MINUTE ))
You can also express the logic using ADDTIME():
SELECT DATE(ADDTIME(currdate(), TIME('12:30:00')))
use NOW
SELECT DATE_ADD( now( ) , INTERVAL '12:30' HOUR_MINUTE )
Related
I'm trying to query my database to get all infrastructures with a due date (inf_lifespan) is between 6 months away from today's date.
inf_lifespan
2028-09-19
2025-12-04
2020-11-11
2026-02-17
2019-12-12
2020-11-13
2018-12-13
Running this query will generate no record but I expect that infrastructure with a due date of 2018-12-13 should be the outcome of the query.
select *
from tbl_infrastructure
where `inf_lifespan` BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 6 MONTH ) AND DATE_SUB( CURDATE( ) ,INTERVAL 0 MONTH )
I m using 5.7.21 - MySQL Community Server (GPL)
Can anyone help me with this, please
In the condition BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 6 MONTH ) AND DATE_SUB( CURDATE( ) ,INTERVAL 0 MONTH )
you will the date from 6 months ago to today.
You can try to use DATE_ADD instead of DATE_SUB, because of DATE_SUB function is sub the month.
select *
from tbl_infrastructure
where `inf_lifespan` BETWEEN CURDATE() AND DATE_ADD( CURDATE(), INTERVAL 6 MONTH)
I am executing the following query
SELECT post_expiredate
FROM tbl_postad
WHERE post_expiredate > DATE_SUB( CURDATE( ) , INTERVAL 1
DAY )
LIMIT 0 , 30
what I expect is, it should show the post_expiredate column with date values within last 24 hours. but it shows the wrong result
Please help me solve this issue
I tried
SELECT post_expiredate
FROM tbl_postad
WHERE DATE( post_expiredate ) > DATE( DATE_SUB( CURDATE( ) , INTERVAL 1
DAY ) )
also this
SELECT post_expiredate
FROM tbl_postad
WHERE post_expiredate > DATE_SUB( NOW( ) , INTERVAL 1
DAY )
but it also shows wrong date/
you did
WHERE post_expiredate > DATE_SUB( NOW( ) , INTERVAL 1
DAY )
which is wrong and list out all dates which are greater than yesterday
you need to find all post between yesterday and today
SELECT post_expiredate
FROM tbl_postad
WHERE post_expiredate between DATE_SUB( now( ) , INTERVAL 1
DAY ) and now()
SELECT post_expiredate
FROM tbl_postad
WHERE DATE(post_expiredate) BETWEEN CURDATE() + INTERVAL 1 DAY AND CURDATE()
this query must show all the "post_expiredate" between today and tomorrow.
Hope it helped.
I have a table of dates, and want to select dates that are in 1,5 hour interval of given date.
For example:
-- '2013-06-11 18:40' is the given date, and I want all dates that are
-- inside +/- 1.5 hour interval
SELECT datum
FROM table
WHERE date(datum) > date( date_sub( '2013-06-11 18:40', interval 1.5 hour ) )
AND date(datum) < date( date_add( '2013-06-11 18:40', interval 1.5 hour ) )
The MySQL date function takes the date part of a datetime. That's not advisable when you're looking for a 3 hour interval :)
Try:
WHERE datum between date_sub( '2013-06-11 18:40', interval 90 minute) and
date_add( '2013-06-11 18:40', interval 90 minute)
I used integer minutes instead of hours plus removed DATE function as Andomar suggested:
SELECT datum
FROM table
WHERE
datum > date( date_sub( '2013-06-11 18:40', interval 90 MINUTE ) )
AND
datum < date( date_add( '2013-06-11 18:40', interval 90 MINUTE ) )
i have a table called "reportinc" where i have to extract -it is VARCHAR field- items from 1 or some days past at 18:01 (time format as %H:%i) to today at 18:00.
I tried with this query
SELECT `ID incidente`, `Data/ora apertura`
FROM `reportinc`
WHERE `Data/ora apertura`
BETWEEN
(
SELECT date_format( now( ) - INTERVAL 1
DAY , "%d/%m/%y at 18:01 " )
)
AND
(
SELECT date_format( now( ) , "%d/%m/%y at 18:00 " )
I want to extract from yesterday at 18:01 to today 18:00.
But the results are form 00:00 yesterday to today.
It dont consider 18:00 or 18_01 as valid time format.
How specify time into query?
MANY THANKS AGAIN
The Data/ora apertura field must be a DATETIME field, or you have to make a CAST.
Then you must do this:
SELECT `ID incidente`, `Data/ora apertura`
FROM `reportinc`
WHERE `Data/ora apertura`
BETWEEN
(
SELECT date_format( now( ) - INTERVAL 1 DAY , "%Y-%m-%d 18:01:00" )
)
AND
(
SELECT date_format( now( ) , "%Y-%m-%d 18:00:00"
)
Between do not work as you expected when the field type is VARCHAR.
At first change the format from varchar to valid date format. varchar takes the value as string and you will have to use regex to bifurcate date and time. Better to go with former option.
EDIT added more specific date time format
I ran in to the same problem the combination between time and date. Time is ignored therefore you get the results form the whole day.
Try PHP or another programming language to manipulate the MySQL out. Or try as I suggest to change the import method of the CSV file.
SELECT *,CAST((STR_TO_DATE(`Data/ora apertura`, '%d/%m/%Y %H.%i')) AS DATETIME)
AS DATE_TIME
FROM reportinc
WHERE (CAST((STR_TO_DATE(`Data/ora apertura`, '%d/%m/%Y %H.%i')) AS DATETIME)) >= ('2012-22-01 21:00');
SELECT *,CAST((STR_TO_DATE(`Data/ora apertura`, '%d/%m/%Y %H.%i')) AS DATETIME)
AS DATE_TIME
FROM reportinc
WHERE (STR_TO_DATE(`Data/ora apertura`, '%d/%m/%Y %H.%i')) >= ('2012-22-01 21:00');
Sample data:
CREATE TABLE reportinc (
`ID_incidente` int auto_increment primary key,
`Data/ora apertura` varchar(30)
);
INSERT INTO reportinc (`Data/ora apertura`)
VALUES
("31/07/2012 10.46"),
("31/07/2012 10.47"),
("22/01/2013 20.00"),
("23/01/2013 10.00");
SQL FIDDLE demo
---
Try something like:
Using the cast function:
SELECT * FROM reportinc WHERE (CAST(`Data/ora apertura` AS DATETIME)) >= NOW() - INTERVAL 3 hour;
Using the str_to_date function:
SELECT * FROM reportinc WHERE (STR_TO_DATE(`Data/ora apertura`,'%Y-%m-%d %H:%i:%s')) >= NOW() - INTERVAL 3 hour;
You are a bit unclear about the time range so I took 3 hours as a example. You can also use a specific date and time.
Using cast:
`SELECT * FROM reportinc WHERE (CAST(`Data/ora apertura` AS DATETIME)) >= ('2013-01-22 20:00:00');`
Using str_to_date
SELECT * FROM reportinc WHERE (STR_TO_DATE(apertura, '%Y-%m-%d %H:%i:%s')) >= ('2013-01-21 20:00:00');
Sample data:
CREATE TABLE reportinc (
`ID_incidente` int auto_increment primary key,
`Data/ora apertura` varchar(30),
`Data/ora apertura1` datetime
);
INSERT INTO reportinc ( `Data/ora apertura`, `Data/ora apertura1`)
VALUES (NOW() - INTERVAL 1 day, NOW() - INTERVAL 1 day),
(NOW() - INTERVAL 1 hour, NOW() - INTERVAL 1 hour),
(NOW() - INTERVAL 3 hour, NOW() - INTERVAL 3 hour),
(NOW() - INTERVAL 6 hour, NOW() - INTERVAL 6 hour),
(NOW() - INTERVAL 9 hour, NOW() - INTERVAL 9 hour),
(NOW() - INTERVAL 1 year, NOW() - INTERVAL 1 year);
SQL FIDDLE DEMO
B.T.W. My advices is to stop using slashes, spaces etc in column and table names.
I would appreciated it if you would accept answers that have helped you.
In MySQL, how would I get a timestamp from, say 30 days ago?
Something like:
select now() - 30
The result should return a timestamp.
DATE_SUB will do part of it depending on what you want
mysql> SELECT DATE_SUB(NOW(), INTERVAL 30 day);
2009-06-07 21:55:09
mysql> SELECT TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 day));
2009-06-07 21:55:09
mysql> SELECT UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 day));
1244433347
You could use:
SELECT unix_timestamp(now()) - unix_timestamp(maketime(_,_,_));
For unix timestamps or:
SELECT addtime(now(),maketime(_,_,_));
For the standard MySQL date format.
If you need negative hours from timestamp
mysql>SELECT now( ) , FROM_UNIXTIME( 1364814799 ) , HOUR( TIMEDIFF( now( ) , FROM_UNIXTIME( 1364814799 ) ) ) , TIMESTAMPDIFF( HOUR , now( ) , FROM_UNIXTIME( 1364814799 ) )
2013-06-19 22:44:15 2013-04-01 14:13:19 1904 -1904
this
TIMESTAMPDIFF( HOUR , now( ) , FROM_UNIXTIME( 1364814799 ) )
will return negative and positive values, if you need to use x>this_timestamp
but this
HOUR( TIMEDIFF( now() , FROM_UNIXTIME( 1364814799 ) ) )
will return only positive, hours