how to sort Date in mysql - mysql

scheduled_datetime
2016-12-07 10:30:00
2016-12-07 13:30:00
2016-11-06 22:30:00
2016-12-06 23:30:00
2016-19-08 22:30:00
sorting first date wise if date is same then sort by time
select *
from mytable
where scheduled_datetime > now()
order by scheduled_datetime;
from this query i have to find date which greater than today time please suggest me where am doing wrong .

If you want to only output dates which are greater than today, then you need to use DATE() to only compare to the date values of scheduled_datetime:
select *
from mytable
where DATE(scheduled_datetime) > NOW()
order by scheduled_datetime asc;

You have following 3 options :
--If you want to fetch records greater than current date & time
select * from mytable where scheduled_datetime > NOW()
--If you want to fetch records greater than current date
select * from mytable where scheduled_datetime > CURDATE()
--If you want to fetch records greater than current time
select * from mytable where scheduled_datetime > CURTIME()
You can use ORDER BY ASC or DESC according, in what order you want to get data.

Related

How do I retrieve all records between a date range and then filtered by time of day?

My dateTime column has entries like this going back months:
2019-03-11 07:00:00.000
I would like to retrieve all the data from mondays, but then further filtered by the time of day.
This is what I have so far but I dont know how to refine it any further.
WITH
cteMonday (dates1, Average) AS
(
select [Date], [High] - [Low] AS 'average'
--select *
from [Trading].[dbo].[DAX30m]
where DATEPART(dw, Date) = 2
AND [DATE] BETWEEN '2019-03-11' AND '2019-03-24'
)
select * from ctemonday
Why don't you use the DATEPART function to extend your filter criteria, e.g.:
... AND DATEPART(hh, Date) > lowerLimit AND DATEPART(hh, Date) <
upperLimit

How to count the numbers of rows within a certain days

I would like to perform a query that counts the number of rows that have a close date within 60 days of open_date by store.
Sample: Table A
Store Open Date Close Date
A 2017-01-01 2017-01-31
B 2017-02-02 Null
A 2017-01-02 2018-01-21
Thanks in advance.
Use datediff()
SELECT count(*) FROM [Table A] WHERE datediff(Close_date,Open_date) <= 60;
Try this on of the queries below:
SELECT count(*) numberOfDays
FROM TableA
WHERE close_date>=CURRENT_DATE - INTERVAL 60 DAY;
SELECT count(*) numberOfDays
FROM TableA
WHERE close_date>=CURRENT_DATE - INTERVAL '60' DAY;
See it work on SQL Fiddle.
select store, open_date, count(*)
from tableA
where close_date between date_sub(open_date, interval 60 DAY) and date_add(open_date, interval 60 DAY)
group by store, open_date;
This will work but your starting date has to either be current date or the open_date (which is what your question was). Given that the open_date appears to be different for each row, your count is going to be ... interesting.
Also, count will always return something, so unless you use an if or case statement keying on 0 (zero) it will return a 0 not a null.

How can I filter a query by the hour portion of a DateTime field in MySQL?

I need to select rows from table, where e.g. time is >= 18:00:00 no matter of date. Problem is that value is datetime type so there is also date beside. e.g. 2012-01-25 18:00:00.
table1
======
row 1: id='1' datetime='2012-01-25 18:00:00'
row 2: id='2' datetime='2012-01-27 15:00:00'
row 3: id='3' datetime='2012-01-30 19:45:00'
I need to select row 1 and row 3.
Is there way to combine LIKE and >= to do time >= '% 18:00:00' where % represents whatever date?
You can use the TIME() function:
WHERE TIME(`datetime`) >= '18:00:00'
select *
from table1
where HOUR(datimetime) >= 18
Something like this maybe:
SELECT *
FROM yourTable
WHERE
EXTRACT(HOUR FROM YourDate)>18

Is it possible to get the next date from an output?

I have a sql-query where I ask for some dates.
"SELECT DISTINCT date FROM table WHERE condition ORDER BY date");
The output is:
2007-04-08
2008-04-12
2008-09-27
2009-12-06
2010-01-31
2011-02-27
2011-04-15
Now I'm wondering whether it is possible to get the next date from the output above.
Examples:
1. Today is 2008-12-12. The next date would be 2009-12-06.
2. Today is today/current_date (2011-02-22). The next date would be 2011-02-27.
Have you got an idea how to specify the query?
Thank you.
SELECT DISTINCT date FROM table WHERE dateColumn > CURDATE()
Order by dateColumn desc
If you want only the NEXT date, then you can use LIMIT
SELECT DISTINCT date FROM table WHERE dateColumn > CURDATE()
Order by dateColumn desc
LIMIT 1

Display rows from MySQL where a datetime is within the next hour

I always have trouble with complicated SQL queries.
This is what I have
$query = '
SELECT id,
name,
info,
date_time
FROM acms_events
WHERE date_time = DATE_SUB(NOW(), INTERVAL 1 HOUR)
AND active = 1
ORDER BY date_time ASC
LIMIT 6
';
I want to get up to 6 rows that are upcoming within the hour. Is my query wrong? It does not seem to get events that are upcoming within the next hour when I test it.
What is the correct syntax for this?
I'm going to postulate that you're looking at a group of records that contain a range of DATETIME values, so you probably want something more like this:
SELECT id,
name,
info,
date_time
FROM acms_events
WHERE date_time < DATE_ADD(NOW(), INTERVAL 1 HOUR)
AND date_time >= NOW()
AND active = 1
ORDER BY date_time ASC
LIMIT 6
Otherwise, your query is looking for records with a date_time of exactly "now + 1 hour". I'm assuming all your dates aren't specific to that particular second. ;)
To clarify a bit, DATE_ADD() and DATE_SUB() return exact timestamps, so your query above roughly translates to something like SELECT ... WHERE date_time = '2010-04-14 23:10:05' ORDER BY ..., which I don't think is what you want.
WHERE date_time = DATE_SUB(NOW(), INTERVAL 1 HOUR)
means date_time equals exactly now minus one hour, which would result in any record exactly one hour old.
Why not use
WHERE TIMEDIFF(date_time, NOW()) < '01:00:00'
AND date_time > NOW()