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

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

Related

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.

mysql fetch record with valid date not 0000-00-00 00:00:00

i am trying to get all valid timestamp records using below query
SELECT * FROM table where mytimestamp IS NOT NULL
my query outputs records with value "0000-00-00 00:00:00"
that id do not want to occurs , what query should be firect in that case
My first question to you would be why are you storing the year zero, 0000-00-00 00:00:00 in your table in the first place?
In any case, you can add a condition to the WHERE clause to filter out this data if you really have it:
SELECT *
FROM table
WHERE mytimestamp IS NOT NULL AND
mytimestamp > '0000-00-00 00:00:00'
This query is getting your desired output, Please try it -
SELECT * FROM table where mytimestamp != '0000-00-00 00:00:00'
It will work:
SELECT *
FROM `table_name`
where `column_name` IS NOT NULL AND `column_name` > '0000-00-00 00:00:00';

MySQL / PHP: Select Where with multiple conditions does not work

I've got a following database entry:
id date start_time
1 2015-12-25 08:00:00
2 2015-12-30 08:00:00
3 2015-12-30 09:00:00
Now I just want to select the date of those entries where both start_time entries 08:00:00 and 09:00:00 exists.
I tried to use this SQL query:
$sqlquery = mysqli_query($myconnection,"SELECT date
FROM mytable
WHERE start_time LIKE '08:00:00'
AND '09:00:00'") or die ("crashed");
But it returns me both dates 2015-12-25 and 2015-12-30. It should only return 2015-12-30 because for this date 08:00:00 and 09:00:00 exists.
I want to select those dates which have an entry for 08:00:00 and 09:00:00 too.
It should not select dates with only an entry for 08:00:00 but none for 09:00:00 and also not those which have an entry for 09:00:00 but none for 08:00:00.
Don't use like for date/time columns. Here, you seem to want between:
SELECT date
FROM mytable
WHERE start_time BETWEEN '08:00:00' AND '09:00:00';
Your original formulation is parsed like this:
WHERE (start_time LIKE '08:00:00') AND '09:00:00'
The second part is a string value in a boolean/integer context. It gets converted to 9, which is always true. So, the where clause ends up being equivalent to:
WHERE start_time = '08:00:00'
EDIT:
Your clarification changes my understanding of the question. If you want days that have both times, use aggregation:
SELECT date
FROM mytable
WHERE start_time IN ('08:00:00', '09:00:00')
GROUP BY date
HAVING COUNT(*) = 2;
i assume that you basically want to select date that has both '08:00:00' and '09:00:00', then you should not use 'BETWEEN'.
try this query:
SELECT t1.date
FROM mytable AS t1
INNER JOIN mytable AS t2 ON t1.date = t2.date
INNER JOIN mytable AS t3 ON t1.date = t3.date
INNER JOIN mytable AS t4 ON t1.date = t4.date
WHERE t1.start_time = '08:00:00'
AND t2.start_time = '09:00:00'
AND t3.start_time = '10:00:00'
AND t4.start_time = '11:00:00'
GROUP BY t1.date
or you can try another approach
SELECT t1.date
FROM mytable AS t1
GROUP BY t1.date
HAVING SUM(IF(t1.start_time = '08:00:00', 1, 0)) > 0
AND SUM(IF(t1.start_time = '09:00:00', 1, 0)) > 0
AND SUM(IF(t1.start_time = '10:00:00', 1, 0)) > 0
AND SUM(IF(t1.start_time = '11:00:00', 1, 0)) > 0
As mentioned in the comments there are different ways to achieve it depending on what you actually want to do with the result.
Easy->Just count the records with specific dates
select date, count(start_time)
from mytable
group by date
having count(start_time) > 1
2.Advanced->Display the records by using a case
select *
from (
Select date,
case when start_time = '08:00:00' then 1 end as startat8,
case when start_time = '09:00:00' then 1 end as startat9
from mytable
) a
where a.startat8=1 and a.startat9=1;

MySQL BETWEEN DATE RANGE

I have a scenario where I need to pull up delivery dates based on a table below (Example)
job_id | delivery_date
1 | 2013-01-12
2 | 2013-01-25
3 | 2013-02-15
What I'm trying to do is show the user all the delivery dates that start with the earliest (in this case it would be 2013-01-12) and add an another 21 days to that. Basically, the output I would expect it to show of course, the earliest date being the starting date 2013-01-12 and 2013-01-25. The dates past the February date are of no importance since they're not in my 21 date range. If it were a 5 day range, for example, then of course 2013-01-25 would not be included and only the earliest date would appear.
Here is main SQL clause I have which only shows jobs starting this year forward:
SELECT date, delivery_date
FROM `job_sheet`
WHERE print_status IS NULL
AND job_sheet.date>'2013-01-01'
Is it possible to accomplish this with 1 SQL query, or must I go with a mix of PHP as well?
You can use the following:
select *
from job_sheet
where print_status IS NULL
and delivery_date >= (select min(delivery_date)
from job_sheet)
and delivery_date <= (select date_add(min(delivery_date), interval 21 day)
from job_sheet)
See SQL Fiddle with Demo
If you are worried about the dates not being correct, if you use a query then it might be best to pass in the start date to your query, then add 21 days to get the end date. Similar to this:
set #a='2013-01-01';
select *
from job_sheet
where delivery_date >= #a
and delivery_date <= date_add(#a, interval 21 day)
See SQL Fiddle with Demo
SELECT date,
delivery_date
FROM job_sheet
WHERE print_status IS NULL
AND job_sheet.date BETWEEN (SELECT MIN(date) FROM job_sheet) AND
(SELECT MIN(date) FROM job_sheet) + INTERVAL 21 DAY
SELECT j.job_id
, j.delivery_date
FROM `job_sheet` j
JOIN ( SELECT MIN(d.delivery_date) AS earliest_date
FROM `job_sheet` d
WHERE d.delivery_date >= '2013-01-01'
) e
ON j.delivery_date >= e.earliest_date
AND j.delivery_date < DATE_ADD(e.earliest_date, INTERVAL 22 DAY)
AND j.print_status IS NULL
ORDER BY j.delivery_date
(The original query has a predicate on job_sheet.date; the query above references the d.delivery_date... change that if it is supposed to be referencing the date column instaed.)
If the intent is to only show delivery_date values from today forward, then change the literal '2013-01-01' to an expression that returns the current date, e.g. DATE(NOW())

SQL and date comparison

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.

Categories