SQL and date comparison - php

I have a sql statement, i select a few items, but I need to make sure that the date i have saved is greater then the current date.
SELECT * FROM table WHERE column_date > current_date
How do I make the code over, working?
Column date is saved like this 0000-00-00 00:00:00 (usual save, in other words).
This is what I try to achieve: AND time > current_date OR time == NULL But it ain't working. The time column is currently 0000-00-00 00:00:00 which is NULL, right?
Or how can I do, it must be greater than or equal to 0000-00-00 00:00:00

If you are using MySQL, NOW() should do the trick.
SELECT * FROM table WHERE column_date > NOW()
If you want to eliminate the time value and just compare to date value, following could be used:
SELECT * FROM table WHERE column_date > CURDATE()

You could use the Mysql Function NOW():
SELECT * FROM table WHERE column_date > NOW()

As per this documentation article, 0000-00-00 is a "dummy date" that can be used instead of NULL, which means 0000-00-00 itself is not NULL.
Given that 0000-00-00 cannot be greater than any other date, you should use either a condition with OR:
SELECT * FROM table WHERE column_date > NOW() OR column_date = '0000-00-00'
or a union:
SELECT * FROM table WHERE column_date > NOW()
UNION ALL
SELECT * FROM table WHERE column_date = '0000-00-00'
Alternatively you could use a construct like this:
SELECT *
FROM table
WHERE IFNULL(NULLIF(column_name, '0000-00-00'), '9999-12-31') > NOW()
But that would probably prohibit the query from taking advantage of the index on column_date, if any.

Related

Query capturing timestamp 0000-00-00 00:00:00

How do I get this query to not pick up timestamps with the default DatePercentUpdate = 0000-00-00 00:00:00? I thought I had it by using 0, but it captures the 0000-00-00 00:00:00's.
$stmtSummary = $mysqliSummary
->prepare("SELECT n.nodeID, n.percentRedYellow, n.count, c.Loopback_ip, c.city
FROM CATV.CableData c
INNER JOIN CATV.node n ON n.nodeID = c.Node_ID
INNER JOIN CATV.CM cm ON cm.LoopbackIP=c.Loopback_ip
WHERE c.LocationID IS NOT NULL
AND c.Status_Update_Time <= NOW()
AND c.Status_Update_Time >=(NOW() - INTERVAL 2 DAY)
AND c.Status_Update_Time>0
AND n.DatePercentUpdate <= NOW()
AND n.DatePercentUpdate >=(NOW() - INTERVAL 2 DAY)
AND n.DatePercentUpdate>0
AND length(c.Node_ID)>0
Group BY c.city, n.nodeID");
Added: I tried searching online to use 0000-00-00 00:00:00 in the query, but didn't find anything with that in a filter.
You can simply use the > (greater than) comparison operator or the != (doesn't equal) comparison operator like so
WHERE `date_field` != '0000-00-00 00:00:00'
The example will get all rows where your date doesn't equal that value.
Capture Query where timestamp is 0000-00-00 00:00:00
To select rows
select * from table_name where your_column = 0;
To update rows
UPDATE table_name SET column_name = new_value where your_column = 0;
This will fix
mysql incorrect datetime value: '0000-00-00 00:00:00' mysql error

Between Operator - Mysqli [duplicate]

I have saved the dates of a user's registration as a datetime, so that's for instance 2011-12-06 10:45:36. I have run this query and I expected this item - 2011-12-06 10:45:36 - will be selected:
SELECT `users`.* FROM `users` WHERE created_at >= '2011-12-01' AND
created_at <= '2011-12-06'
But is not. Exist any elegant way, how to select this item? As a first idea that I got was like 2011-12-06 + 1, but this doesn't looks very nice.
Your problem is that the short version of dates uses midnight as the default. So your query is actually:
SELECT users.* FROM users
WHERE created_at >= '2011-12-01 00:00:00'
AND created_at <= '2011-12-06 00:00:00'
This is why you aren't seeing the record for 10:45.
Change it to:
SELECT users.* FROM users
WHERE created_at >= '2011-12-01'
AND created_at <= '2011-12-07'
You can also use:
SELECT users.* from users
WHERE created_at >= '2011-12-01'
AND created_at <= date_add('2011-12-01', INTERVAL 7 DAY)
Which will select all users in the same interval you are looking for.
You might also find the BETWEEN operator more readable:
SELECT users.* from users
WHERE created_at BETWEEN('2011-12-01', date_add('2011-12-01', INTERVAL 7 DAY));
SELECT users.* FROM users WHERE created_at BETWEEN '2011-12-01' AND '2011-12-07';
You need to use '2011-12-07' as the end point as a date without a time default to time 00:00:00.
So what you have actually written is interpreted as:
SELECT users.*
FROM users
WHERE created_at >= '2011-12-01 00:00:00'
AND created_at <= '2011-12-06 00:00:00'
And your time stamp is: 2011-12-06 10:45:36 which is not between those points.
Change this too:
SELECT users.*
FROM users
WHERE created_at >= '2011-12-01' -- Implied 00:00:00
AND created_at < '2011-12-07' -- Implied 00:00:00 and smaller than
-- thus any time on 06
Another alternative is to use DATE() function on the left hand operand as shown below
SELECT users.* FROM users WHERE DATE(created_at) BETWEEN '2011-12-01' AND '2011-12-06'
Have you tried before and after rather than >= and <=? Also, is this a date or a timestamp?
Searching for created_at <= '2011-12-06' will search for any records that where created at or before midnight on 2011-12-06
. You want to search for created_at < '2011-12-07'.
Maybe use in between better. It worked for me to get range then filter it
You can use MySQL DATE function like below
For instance, if you want results between 2017-09-05 till 2017-09-09
SELECT DATE(timestamp_field) as date FROM stocks_annc WHERE DATE(timestamp_field) >= '2017-09-05' AND DATE(timestamp_field) <= '2017-09-09'
Make sure to wrap the dates within single quotation ''
Edit:
A better solution would be this. It would make sure that it uses the index if any exists.
select date(timestamp_field) as date from stocks_annc where time_stamp_field >= '2022-01-01 00:00:00' and time_stamp_field <= '2022-01-10 00:00:00'
Hope this helps.

select timestamp from mysql

I have a problem with query where the conditions are related to TIMESTAMP column. So I have a table named table1 which has two columns start_time and end_time. Both of them store TIMESTAMP. I want to query my databse for a particular rows where end_time is higher than given timestamp value and lower than given timestamp value. Basically I want to get all the rows where end_time is between given TIMESTAMP range.
I have found that in my case following query works:
SELECT * FROM table1
WHERE UNIX_TIMESTAMP(end_time) >= FROM_UNIXTIME('2015-11-30 20:14:00')
AND UNIX_TIMESTAMP(end_time) <= FROM_UNIXTIME('2015-11-30 20:14:05')
But it is not what I want to. I want to give TIMESTAMP value instead of DATE.
What is also strange is that this query returns values that have end_time of earlier date than given timestamp:
SELECT * FROM table1 WHERE end_time > FROM_UNIXTIME( '2015-11-30 20:20:05' )
Query above returns rows where end_timestamp is 2015-11-30 20:18:05
I feel that I am missing something here. Should I use INT instead of TIMESTAMP for my end_time? I believe that the following query should return expected values but it gives me nothing:
SELECT * FROM table1
WHERE end_time >= FROM_UNIXTIME( 1448914740 )
AND end_time <= FROM_UNIXTIME( 1448914810 )
You don't need to convert your WHERE clause values using FROM_UNIXTIME. FROM_UNIXTIME converts a TIMESTAMP into a date. If you're sending values to the SQL statement that are already in TIMESTAMP format, your SQL will look like this:
SELECT * FROM table1 WHERE end_time >= 1448914740 AND end_time <= 1448914810
If you are sending date strings (e.g. '2015-11-30 20:14:00' and '2015-11-30 20:14:05') then your SQL would look like this:
SELECT * FROM table1 WHERE FROM_UNIXTIME(end_time) >= '2015-11-30 20:14:00' AND FROM_UNIXTIME(end_time) <= '2015-11-30 20:14:05'
Since you're storing your values in your table as TIMESTAMP I would recommend you use the first SQL statement.

Comparing dates and times in different fields

Now, I've a problem with the following query:
SELECT *
FROM table
WHERE date > CURDATE()
OR date = CURDATE()
AND time > CURTIME()
It's return rows with date > of today but I need also rows with date of today but with time > of the current time.
You need to put the related clauses inside parenthesis:
SELECT *
FROM table
WHERE date > CURDATE()
OR (
date = CURDATE()
AND time > CURTIME()
)
You should use the appropriate date/time functions instead of complicating yourself with complex WHERE clauses:
SELECT * FROM TABLE
WHERE ADDTIME(date, time) > NOW()
More information on the ADDTIME function in this link.

Select records from current month

I have a table with a varchar field called slldate. This field contains dates in this format:
2010-08-30
(YYYY-MM-DD)
Now I would like to select the records containing the current month using a mysql query.
Anyone?
as you used a char field instead a date field, you have to cast the value and then use the normal date functions. like
SELECT * FROM table WHERE MONTH(CAST(slidate as date)) = MONTH(NOW()) AND YEAR(CAST(slidate as date)) = YEAR(NOW())
Try this:
SELECT * FROM table_name WHERE MONTH(slldate) = date('m') AND YEAR(slldate) = date('Y');
I have a table with a varchar field called slldate. This field contains dates
Then you should change it to a date field. You (should) know it's the right thing to do.
The method below will work with your varchar strings - but can be simplified somewhat for dates.
Note that since your date strings are already big-endian, you don't need to cast them and loose the benefit of index optinmisation:
SELECT *
FROM yourtable
WHERE slldate >= CONCAT(DATE_FORMAT('%Y-%m-', NOW()),'01')
AND slldate < CONCAT(DATE_FORMAT('%Y-%m-', NOW() + INTERVAL 1 MONTH), '01')
SELECT * FROM table_name WHERE YEAR(savedate) = YEAR(CURDATE()) AND MONTH(savedate) = MONTH(CURDATE())

Categories