Select datetime according to HH:MM condition - mysql

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.

Related

How to compare SQL time column to time now?

So I have a column with time type and the entries are saved as 12:15:00, what I'd like to do is get
SELECT * FROM table WHERE time > NOW() however this doesn't work because NOW() returns a datetime not just time. Is there a way to do this?
You can use the time() function or curtime():
SELECT *
FROM table
WHERE time > curtime();

Read data stored as text and use in IF statement

I have a column that stores dates as text, I need to select all the entries with date less than the date of today.
If I use this:
SELECT *
FROM mytab
WHERE expire < CURRENT_DATE( )
ORDER BY expire DESC
It doesn't select the correct entries but only the ones with da_expire empty.
How can I fix it?
In the first place, why are you storing it as string?
You need to convert it to date using MySQL's builtin function so you can be able to compare it with today's date.
WHERE STR_TO_DATE(expire, '%Y/%m/%d %H:%i') < CURDATE()
This will be a little slower since it will not use any index if you have one defined on the column.
MySQL Docs: STR_TO_DATE()
Use STR_TO_DATE(expire, '%m/%d/%Y') instead of expire in the query. I have assumed you are storing the date in month day year format. You will need to adjust the format as per the string format. However, for performance reasons convert the type of expire during load/insert process .

MySQL date validity

Does MySQL provide any function which verifies the validity of a date? The DATE function returns NULL upon provision of the invalid date 2013-02-30 for example. However, I am also using STR_TO_DATE simultaneously, which mysteriously stops DATE from working correctly.
SELECT DATE('2013-02-30'); NULL
SELECT STR_TO_DATE('2013-02-30', '%Y-%m-%d'); NOT NULL
SELECT DATE('2013-02-40'); NULL
SELECT STR_TO_DATE('2013-02-40', '%Y-%m-%d'); NULL
SELECT DATE(STR_TO_DATE('2013-02-30', '%Y-%m-%d')); NOT NULL
Why does STR_TO_DATE halt DATE's functionality and is there some workaround to verify if a date is valid when using STR_TO_DATE (which I am obligated to use)?
I have stumbled upon the answer in the meantime: apparently the DATE function skips a few validation checks, when the data type is already that of 'date' (STR_TO_DATE converts strings to date data types). Therefore, converting the date to a string after having parsed it to the correct format with STR_TO_DATE, does the trick:
#valid_date = NOT ISNULL(DATE(CONVERT(STR_TO_DATE('2013-02-29', '%Y-%m-%d'), CHAR))).
It is very difficult to verify if a field is a date because of all the different possible date formats that would need to be taken into account. BUT if you know that the field date formats are one of these:
'yyyy-mm-dd'
'yyyy-mm-dd hh:mm:ss'
'yyyy-mm-dd whatever'
This code will help you:
SELECT count(*) FROM `table`
WHERE DATE(STR_TO_DATE(`column`, '%Y-%m-%d')) IS NOT NULL
AND `column` NOT REGEXP '^[0-9\.]+$'
Basically :
the first condition tells you if is a date, but unfortunately doesn't exclude numbers (ex: DATE(STR_TO_DATE(**1**, '%Y-%m-%d')) = '2001-00-00'
the second ensures that numbers are excluded, which leaves you with dates only that follow the formats above.
If count(*) is >0 then it's a date, if it is 0 it's something else.
Note
This method works for strings of any date format as long as you know in advance what format they follow (which I know is not always the case but still helpful). Just replace the format a priori in STR_TO_DATE
i can't understand your purpose clearly, maybe this is a idea;
SELECT DATE_FORMAT(yourFiled, '%Y-%m-%d') days FROM yourTable GROUP BY days;
this is not null; you can change it. some like
SELECT DATE_FORMAT(yourFiled, '%Y-%m-%d') days FROM yourTable WHERE yourFiled > '2013-9-23 00:00:00' GROUP By days;
Try this:
SELECT DATE(STR_TO_DATE('2013-00-30', '%Y-%m-%d')); --is also NOT NULL

Mysql datetime between two columns

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.

how do I select on date equality in a mySQL query?

I want to select rows from a table given a particular date of record in mysql
SELECT * from TABLENAME WHERE FROM_DATE='06/11/2012'
I am not getting anything useful.
First of all, you should use the standard date format Y-m-d - otherwise you have to make some nasty queries and sorting is a real b*tch.
Using the standard date format you can easily do something like this:
SELECT * FROM tablename WHERE from_date > '2012-06-11'
DATE comparisons are very likely what you want here. If your from_date column has the data type of DATE, then your code should be safe and robust if you do this:
WHERE from_date = STR_TO_DATE('06/11/2012', '%m/%d/%Y')
#Repox pointed out that you might consider putting your date literals in the canonical format '2012-06-11'. That's true, if you can do it. But STR_TO_DATE will do it for you if you need it to. There's a list of the %x conversion items here. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
If you're using DATETIME data types, beware: comparisons are more complex than they seem. DATETIME items are like floating point numbers: if one of them exactly equals another it's only by coincidence. That's because they represent moments (milliseconds) in time, not just days.
Presuming your from_date column has the DATETIME type, you should use
WHERE from_date >= STR_TO_DATE('06/11/2012', '%m/%d/%Y')
AND from_date < STR_TO_DATE('06/11/2012', '%m/%d/%Y') + INTERVAL 1 DAY
This will catch all moments in time on the day you want, up to but not including the first moment of the next day.
If your from_date items are represented as character strings, take the trouble to convert them to DATE or DATETIME data types. Seriously. Your results will be far better.
SELECT * from TABLENAME WHERE FROM_DATE='2012/06/13'
It would be better if you use the DATE() function of mysql
SELECT * FROM tablename WHERE DATE(from_date) > '2012-06-11'
Because, if the datatype of the from_date you set as TIMESTAMP or DATETIME then it won't return the correct results sometimes when you directly use the '>' symbol