Select data between a date/time range - mysql

How do I select data between a date range in MySQL. My datetime column is in 24-hour zulu time format.
select * from hockey_stats
where game_date between '11/3/2012 00:00:00' and '11/5/2012 23:59:00'
order by game_date desc;
Returns nothing despite having data between these time periods. Do I have to force the values in the 'from' and 'to' fields to datetime type in the query?

You need to update the date format:
select * from hockey_stats
where game_date between '2012-03-11 00:00:00' and '2012-05-11 23:59:00'
order by game_date desc;

Here is a simple way using the date function:
select *
from hockey_stats
where date(game_date) between date('2012-11-03') and date('2012-11-05')
order by game_date desc

A simple way :
select * from hockey_stats
where game_date >= '2012-03-11' and game_date <= '2012-05-11'

You probably need to use STR_TO_DATE function:
select * from hockey_stats
where
game_date between STR_TO_DATE('11/3/2012 00:00:00', '%c/%e/%Y %H:%i:%s')
and STR_TO_DATE('11/5/2012 23:59:00', '%c/%e/%Y %H:%i:%s')
order by game_date desc;
(if game_date is a string, you might need to use STR_TO_DATE on it)

In a simple way it can be queried as
select * from hockey_stats
where game_date between '2018-01-01' and '2018-01-31';
This works if time is not a concern.
Considering time also follow in the following way:
select * from hockey_stats where (game_date between '2018-02-05 01:20:00' and '2018-02-05 03:50:00');
Note this is for MySQL server.

MySQL date format is this : Y-M-D. You are using Y/M/D. That's is wrong.
modify your query.
If you insert the date like Y/M/D, It will be insert null value in the database.
If you are using PHP and date you are getting from the form is like this Y/M/D, you can replace this with using the statement .
out_date=date('Y-m-d', strtotime(str_replace('/', '-', $data["input_date"])))

You can either user STR_TO_DATE function and pass your own date parameters based on the format you have posted :
select * from hockey_stats where game_date
between STR_TO_DATE('11/3/2012 00:00:00', '%c/%e/%Y %H:%i:%s')
and STR_TO_DATE('11/5/2012 23:59:00', '%c/%e/%Y %H:%i:%s')
order by game_date desc;
Or just use the format which MySQL handles dates YYYY:MM:DD HH:mm:SS and have the query as
select * from hockey_stats where game_date between '2012-03-11 00:00:00' and'2012-05-11 23:59:00' order by game_date desc;

You must search date defend on how you insert that game_date data on your database.. for example if you inserted date value on long date or short.
SELECT * FROM hockey_stats WHERE game_date >= "6/11/2018" AND game_date <= "6/17/2018"
You can also use BETWEEN:
SELECT * FROM hockey_stats WHERE game_date BETWEEN "6/11/2018" AND "6/17/2018"
simple as that.

Related

Search Date (now) in Specific time

I want to search a Date (now) like 2022-04-11 in specific time like 00:00:00. and in this code showing date & time 0000-00-00 00:00:00.
SELECT Process_Date, Product_Number, COUNT(*) AS TEST FROM tb_main WHERE Process_Date = DATE_FORMAT(NOW(), '00:00:00') GROUP BY Product_Number;
I have already tried this code but syntax was error.
CURDATE(), '00:00:00'
If you want to use DATE_FORMAT to get midnight of the current date, then you need a format mask for the entire timestamp:
SELECT Process_Date, Product_Number, COUNT(*) AS TEST
FROM tb_main
WHERE Process_Date = DATE_FORMAT(NOW(), '%Y-%m-%d 00:00:00')
GROUP BY Product_Number;
You could also use the TIMESTAMP function here:
SELECT Process_Date, Product_Number, COUNT(*) AS TEST
FROM tb_main
WHERE Process_Date = TIMESTAMP(CURRENT_DATE)
GROUP BY Product_Number;

Filter the lowest values of a sql result every time a column changes

Sorry for weird title its hard for me to explain what i need but i will do my best,
My table BehaviorHidraulics contains these columns:
[id],[siteIDDatagate],[datetime],[value],[channelnum],[channeltype], now i need to find the lowest value of [value] in a certain date and i've managed to do so with this query:
select min(value) as minvalue, siteIDDatagate, channeltype, channelnum, datetime
from BehaviorHidraulic where channelnum = '1' and datetime
between '2021-10-10 00:00:00' and '2021-10-10 23:59:59'
group by siteIDDatagate, channeltype, channelnum, datetime order by siteIDDatagate, minvalue ASC
now this returns something like this:
minvalue siteIDDatagate
26 _site_1003
26,39 _site_1003
26,5 _site_1003
17,20 _site_303
17,600 _site_303
58,200 _site_304
58,599 _site_304
and this is good but i need to ditch every result under the first row for every site so it could look like this:
minvalue siteIDDatagate
26 _site_1003
17,20 _site_303
58,200 _site_304
i think i need to use the lag function but im not very good with SQL so please any help is greatly appreciated
I don't think the lag function will help you in this case. The lag function is a windowed function that allows you to access data in a previous row. Akina, posted what I think is the answer for you.
Currently your query is:
select min(value) as minvalue, siteIDDatagate, channeltype, channelnum, datetime
from BehaviorHidraulic where channelnum = '1' and datetime
between '2021-10-10 00:00:00' and '2021-10-10 23:59:59'
group by siteIDDatagate, channeltype, channelnum, datetime order by siteIDDatagate, minvalue ASC
The additional "group by" fields that you have are causing the excess rows. Your results seem to only care about min(value) and siteIDDatagate yet you're grouping on additional fields unnecessarily. You should consider rewriting the query as such:
select min(value) as minvalue, siteIDDatagate
from BehaviorHidraulic where channelnum = '1' and datetime
between '2021-10-10 00:00:00' and '2021-10-10 23:59:59'
group by siteIDDatagate order by siteIDDatagate, minvalue ASC
This will ensure that you only retrieve the minimum value over the date period specified for each siteIDDatagate.
If you shave other requirements, please specify.
You can use row_number() window function to find the first record (the one with minimum value). Here an is an exmaple
;with cte AS
(
select siteIDDatagate,
channeltype,
channelnum,
datetime,
value,
row_number() over(PARTITION BY siteIDDatagate, channeltype, channelnum ORDER BY value) AS RN
from BehaviorHidraulic where channelnum = '1'
and datetime between '2021-10-10 00:00:00' and '2021-10-10 23:59:59'
)
SELECT *
FROM cte
WHERE RN = 1

How to select the rows with maximum timestamp for each month?

How can I select the maximum timestamp for each month?
I think this Query will do the job.
SELECT
YEAR(START_DATE),
MONTH(START_DATE),
MAX(IFNULL(END_DATE, '9999-12-31 23:59:59'))
GROUP BY YEAR(START_DATE), MONTH(START_DATE)
This will work for you, fairly efficiently given the correct indexes.
SELECT LAST_DAY(start_date) month_ending,
MAX(end_date) max_end_date
FROM tbl
GROUP BY LAST_DAY(start_date)

Sql between two dates

I have this query:
SELECT *
FROM some_table
WHERE id != 1 AND
(event_date BETWEEN '20/06/2015' AND '01/07/2015')
ORDER BY the_date
The result is 0.
If I try this query:
SELECT *
FROM events
WHERE id != 1 AND
(event_date BETWEEN '20/06/2015' AND '29/06/2015')
ORDER BY the_date
It works.
What is the problem with the difference between tow month (Juny and July).
Thanks.
Seems like you're comparing these dates lexicographically. Assuming that event_date is a date column, use str_to_date to convert the string literals to dates:
SELECT *
FROM some_table
WHERE id != 1 AND
(event_date BETWEEN STR_TO_DATE('20/06/2015', '%d/%m/%Y') AND
STR_TO_DATE('01/07/2015', '%d/%m/%Y'))
ORDER BY the_date
Write date constants in the ISO standard YYYY-MM-DD format:
SELECT *
FROM some_table
WHERE id != 1 AND
event_date BETWEEN '2015-06-20' AND '2015-07-01'
ORDER BY the_date
MySQL uses the following data types for saving a date:
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MI:SS
TIMESTAMP - format: YYYY-MM-DD HH:MI:SS
Try changing the date format

How to convert a mysql datetime column to just date?

Here is an explanation of how my table is set up (in sqlfiddle)
http://sqlfiddle.com/#!2/86a54d/2
Basically, I have two columns. One is a varchar and contains a name. The other is a datetime and contains a certain date with a time.
I want to select rows where the date in the datetime matches today's date. I have tried things like...
select * from alerts_to_send where date_to_send = now()
select * from alerts_to_send where date_to_send = curdate()
I'm not exactly where to go from here... Any ideas would be greatly appreciated :)
Just to re-clarify, I want to select the rows where today's date matches the date part of my datetime type column.
try this
select * from alerts_to_send
where DATE_FORMAT(date_to_send,'%m-%d-%Y') = DATE_FORMAT(now(),'%m-%d-%Y')
fiddle
You can use the date_format function for mysql
select * from alerts_to_send where date_format(date_to_send, "%m/%d/%Y") = date_format(now(), "%m/%d/%Y")
Here is reference to date_format: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
An alternative to date_format function you can convert the datetime to date:
select * from alerts_to_send where cast(date_to_send as date) = curdate()
It could be even shorter:
select * from alerts_to_send where date(date_to_send) = date(now())
But you will have problem with using index on it so better will be:
select * from alerts_to_send where date_to_send >= date(now()) and date_to_send < date(now()) + INTERVAL 1 DAY