Mysql DATE_FORMAT and timing - mysql

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.

Related

Inserting one MySQL row with values from multiple, nested SELECT statements

I'm trying to combine the following two statements into one SELECT statement, so that I can insert the returned values into one and the same row with one statement.
SELECT ROUND(AVG(pressure),1) FROM rawinput WHERE timestamp >= SUBDATE(timestamp(now()), INTERVAL 1 HOUR);
SELECT ROUND((SELECT AVG(hourly_average_pressure) FROM hour_numbers WHERE timestamp >= CURTIME() - INTERVAL 1 HOUR)-(SELECT AVG(hourly_average_pressure) FROM hour_numbers WHERE timestamp >= CURTIME() - INTERVAL 2 HOUR AND timestamp < CURTIME() - INTERVAL 1 HOUR),1);
I already tried to format like below (basically adding parentheses around both SELECT statements and replace the semicolon with a comma) but I got syntax error. Is the only way to do this using a UNION ALL or am I missing something in the format? Thanks for the help.
(SELECT ROUND(AVG(pressure),1) FROM rawinput WHERE timestamp >=SUBDATE(timestamp(now()), INTERVAL 1 HOUR)), (SELECT ROUND((SELECT AVG(hourly_average_pressure) FROM hour_numbers WHERE timestamp >= CURTIME() - INTERVAL 1 HOUR)-(SELECT AVG(hourly_average_pressure) FROM hour_numbers WHERE timestamp >= CURTIME() - INTERVAL 2 HOUR AND timestamp < CURTIME() - INTERVAL 1 HOUR),1));
If you format your code properly, it's easy to see that you are missing SELECT statement in front of your code.
(
SELECT ROUND(AVG(pressure),1)
FROM rawinput
WHERE timestamp >=SUBDATE(timestamp(now()), INTERVAL 1 HOUR)
)
, (
SELECT ROUND((SELECT AVG(hourly_average_pressure) FROM hour_numbers WHERE timestamp >= CURTIME() - INTERVAL 1 HOUR)-(SELECT AVG(hourly_average_pressure)
FROM hour_numbers
WHERE timestamp >= CURTIME() - INTERVAL 2 HOUR AND timestamp < CURTIME() - INTERVAL 1 HOUR),1)
);
With SELECT
SELECT (
SELECT ROUND(AVG(pressure),1)
FROM rawinput
WHERE timestamp >=SUBDATE(timestamp(now()), INTERVAL 1 HOUR)
)
, (
SELECT ROUND((
SELECT AVG(hourly_average_pressure)
FROM hour_numbers
WHERE timestamp >= CURTIME() - INTERVAL 1 HOUR
) - (
SELECT AVG(hourly_average_pressure)
FROM hour_numbers
WHERE timestamp >= CURTIME() - INTERVAL 2 HOUR AND timestamp < CURTIME() - INTERVAL 1 HOUR
), 1)
);

how to add hours and minutes in curdate mysql

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 )

How to query date range using between in Varchar(10) format mm/dd/yyyy

I am trying to only show records where "DateDue" is within the last year. The trouble is my date is in the format of Varchar(10) as a result of my data feeds coming into my database on intervals from a vendor. Obviously, the dates are not being filtered properly using this query. Is it possible to convert this (temporarily for sake of comparing the date range) to the proper date data type for use in the query, but not permanently alter the type for that column?
SELECT `DocNum`, `DateDue`
FROM `prod_po_list`
WHERE `DateDue` BETWEEN (DATE_FORMAT(curdate( ) - INTERVAL 365 DAY , '%m/%d/%Y' ))
AND DATE_FORMAT( CURDATE( ) , '%m/%d/%Y' )
Are you looking for something like this?
SELECT DocNum, DateDue
FROM prod_po_list
WHERE STR_TO_DATE(DateDue, '%m/%d/%Y')
BETWEEN DATE_SUB(CURDATE(), INTERVAL 365 DAY)
AND CURDATE()
If you have index on date column this will be helpful
SELECT DocNum, DateDue
FROM prod_po_list
WHERE DateDue >= DATE_SUB(CURDATE(), INTERVAL 365 DAY)
AND DateDue <= CURDATE()

MYSQL 30 days query

i have this kind of query:
SELECT COUNT( * )
FROM `reportinc`
WHERE `Data/ora apertura` >= '21/01/13 00:00:00'
AND `Data/ora apertura` <= '21/01/13 18:00:00'
I have to repeat this query for 30 days past from today.
How automate it?
Instead of daytime: 21/01/13 i have to insert something like TODAY -1, TODAY -2 etc etc but my own specified timestamp.
How to?
SELECT TO_DAYS(NOW()) - TO_DAYS(your_table_column_name) <= 30;
Or use DATEDIFF
mysql> SELECT DATEDIFF('1997-12-31 23:59:59','1997-12-30');
-> 1
You could use BETWEEN
SELECT COUNT( * )
FROM `reportinc`
WHERE `Data/ora apertura`
BETWEEN
CURRENT_DATE - INTERVAL 2 DAY
AND
CURRENT_DATE - INTERVAL 1 DAY + '18:00:00'
Something like this:
WHERE `Data/ora apertura` >= CURRENT_DATE - INTERVAL 2 DAY
AND `Data/ora apertura` <= CURRENT_DATE - INTERVAL 1 DAY
+ INTERVAL '18:00:00' HOUR_SECOND

truncate now() to only hour in mysql

How do I truncate now() to get only upto hour precision in mysql?
In
select id from Run where submitTs <= now() and submitTs >= (now() - interval 1 hour);
I really want the records from 12.00 PM to 1 PM instead of 12.23 PM to 1.23 PM if I am running at 1.23 PM.
So how do I say
submitTs<= truncate_to_hour(now())
Try thsi query -
SELECT
id
FROM
Run
WHERE
DATE(submitTs) = CURDATE() AND
EXTRACT(HOUR FROM NOW()) = EXTRACT(HOUR FROM submitTs)
This query has better performance than first one -
SELECT
id
FROM
Run
WHERE
submitTs >= CURDATE() + INTERVAL EXTRACT(HOUR FROM NOW()) HOUR AND
submitTs < CURDATE() + INTERVAL EXTRACT(HOUR FROM NOW()) + 1 HOUR
something like this should work:
SELECT id
FROM Run
WHERE submitTs BETWEEN CAST(CONCAT(DATE_FORMAT(NOW(), '%Y-%m-%d %h'), ':00:00') AS DATETIME)
AND CAST(CONCAT(DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 1 HOUR), '%Y-%m-%d %h'), ':00:00') AS DATETIME);
I think some thing like this can help you to get hour of now():
HOUR(TIME(NOW())
Please see the reference at http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html to get more date-time function in mysql.