I've got two columns (both datetime) startDate and endDate in an events table.
I am retrieving the current day using the the date() function of php.
This results in for example 2013-03-12.
Now there are three possibilities of events combined with dates that occur today:
An event starts and also end on this day
An event has started earlier and ends today
An event starts today but ends in the future (>= 2013-03-13)
Now I'd like to break these all into separate queries as I'm not used to work with dates. I started with the first query, but I am already failing on that one. I've tried the following:
SELECT * FROM events WHERE (startDate= '2013-03-12' AND endDate= '2013-03-12')
aswell as:
SELECT * FROM events WHERE NOT (startDate < '2013-03-12' OR endDate > '2013-03-12')
I've tried to use DATE() aswell and to format dates like '2013-03-12%'.
I don't know why it doesn't work while i am sure there is at least 1 event that is taking place on the 12th. Any help is appreciated.
Try using the MySQL's DATE() function to trim the date columns to the just the date parts:
SELECT *
FROM events
WHERE (DATE(startDate) = '2013-03-12' AND DATE(endDate)= '2013-03-12')
You can use the DATE() function as other answers suggested, but I think this makes it hard to use an index on the columns. Instead, you can include times in your comparisons:
SELECT *
FROM events
WHERE startDate BETWEEN '2013-03-12 00:00:00' AND '2013-03-12 23:59:59'
AND endDate BETWEEN '2013-03-12 00:00:00' AND '2013-03-12 23:59:59'
The DATETIME datatype in MySQL considers the time of the day as well, so, it will not match anything.
If you don't have any use of the time part, then you can simply reduce the datatype to DATE instead of DATETIME. If not, you can use the DATE() function to get rid of the time part and only consider the date part
NOTE THIS WHEN USING between on Mysql
date_column_name between 'startDate' AND 'endDate'
NOTE : you should want to insert +1 date to endDate . Because of when you insert 2015-05-18 date to endDate.you can not get data of 2015-05-18.So you need to plus one date to endDate.
Related
How to find the day difference between today's date and a specified date in SQL. The specified date column(P.SubscrpEndDate__c) is in Date time format(12/1/2014 12:00 Am). I have used the below query but it do not works
DATEDIFF(day,GETDATE(), P.SubscrpEndDate__c) AS 'SubscriptionDueDate'
In MySQL, DATEDIFF function, takes only two arguments: end date and start date:
DATEDIFF(NOW(), P.SubscrpEndDate__c) AS 'SubscriptionDueDate'
According to the manual:
DATEDIFF(expr1, expr2) returns expr1 − expr2 expressed as a value in days from
one date to the other.
Also, to get the current date and time you have to use NOW() instead of GETDATE.
It should work in SQL Server. I assume date column(SubscrpEndDate__c) in your table would contain lower values than current date, so simply you can use query below. I've just swapped second & third parameters to get positive difference in days. You can also use ABS() function to ignore negative difference.
SELECT DATEDIFF(day,P.SubscrpEndDate__c,GETDATE() ) AS 'SubscriptionDueDate'
This would work, kindly check
SELECT DATEDIFF(NOW(),P.SubscrpEndDate__c) AS SubscriptionDueDate FROM xyz_table WHERE id='123456'
I have a table with event and datetime. How can I simply select all events that occurred any day before 21:15 hr?
You can use the TIME() function to get just the time portion of a DateTime column and do the comparison that way:
SELECT *
FROM myTable
WHERE TIME(dateColumn) < '21:15:00';
A full list of useful MySQL date and time functions can be found at this link, which may be helpful for other comparisons in the future.
I have a DateTime column in a MySQL table that I'm trying to filter just from the time of the DateTime. So for example I need all rows that the time is 8am-9am on any date. Is this possible? I've looked through all the docs and can't find anything of the sort.
Thanks guys!
Use the TIME function, like this
where TIME(myColumn) >= '08:00:00' and TIME(myColumn) <= '09:00:00'
TIME gets the time part of your value.
I have a column with timestamp, contain example value "2014-04-16 18:00:00","2014-04-17 18:00:00"....
Now, if I will call a page before "2014-04-17 12:00:00" I need this value-"2014-04-16 18:00:00"
And if I call my page after "2014-04-17 12:00:00" I need this value "2014-04-17 18:00:00".
I think my question is very complicated to understand, having complications in date & times, please check date & time properly.
I want to fetch this data from DB in mysql, The page I was saying is that where I'm going to add your mysql query.
Thanx in advance
Generalising what your asking for a bit the following will return dates from the previous day if it's before noon and dates from today if it's after noon:
SELECT date_column
FROM yourTable
WHERE DATE(DATE_SUB(NOW(), INTERVAL 12 HOUR)) = DATE(date_column);
Edit:
The WHERE clause First gets the current time (NOW()) and subtracts 12 hours. This wont affect the date unless the time is before 12. This means DATE_SUB(NOW(), INTERVAL 12 HOUR) gives us today if it's after noon and yesterday if it's before.
We then check if the date_column matches the date we've created (using the DATE function so that the time is ignored).
Adding some rows to the SELECT may help you see how these dates are built up.
I'm trying to build a query where it will return all rows from today's date, but only up to this point in time of the day.
E.G.
If it's 12.30pm, I'd like it to return all rows with the date from 00:00 to 12:30 on today's date, but nothing after 12:30, or whatever time is when the scrips is run
DATE(expression) removes the time and NOW() includes the current time:
SELECT * FROM tbl WHERE iDate > DATE(NOW()) and iDate < NOW()
have you tried:
SELECT * FROM table WHERE date < TODAY && date > DAYBEFORE
something like this should work fine for timestamp saved dates. Not sure about DATE formated values
You can specify a starting timestamp and an ending timestamp:
where created >= timestamp_start && created < timestamp_end
Note that my answer assumes you're using some server side programming language to run these sql queries, and thus you would be passing a variable into timestamp_start and timestamp_end. time format: use Unix Timestamp