I work for a call center doing tech support, our Dialer uses filters that are written in MySQL. I am a complete novice and cannot wrap my head around it no matter how many tutorials I try.
The only example I can find in the manual is:
called count >= 7
This will only call leads with 7 or greater attempts.
The above syntax redacts the SELECT and WHERE statements because apparently the filter is merely a WHERE statement appended to the standard query.
We have a field called entry_date it is in the format of 11/17/2015 9:48:16 AM (as are all our date entries) I am attempting to filter by results greater than 30 days from the current date.
I tried
entry_date - NOW() >= 30
And it doesn't work :(
Any assistance would be most appreciated.
Ryan
Here is a real example of a VICIdial filter that is used to prevent NI(Not Interested) status leads from being dialed within 30 days of their last call:
( ( (status='NI') and (last_local_call_time < CONCAT(DATE_ADD(CURDATE(), INTERVAL -30 DAY),' ',CURTIME()) ) ) or (status != 'NI') )
You may have an issue with your date format if it is stored in the database as you indicate. The following will select within the last 30 days if entry_date is a datetime field.
entry_date > NOW() - INTERVAL 30 DAY
yes, well, the mysql date functions are a bit bizarre. Here is the way to compute the date 30 days in the past:
mysql> select DATE_SUB(CURDATE(), INTERVAL 30 day) as past;
+------------+
| past |
+------------+
| 2015-10-20 |
+------------+
and I guess you will need to convert the timestamp of your entry_date field to just a plain date without the time, something like this:
DATE(entry_date)
so then I would guess that to get all records older than 30 days would be:
DATE(entry_date) < DATE_SUB(CURDATE(), INTERVAL 30 day)
As suggested by #Sean, MySQL datetime format is yyyy-mm-dd hh:mm:ss
so you will first have to conver your datetime MySQL datetime using STR_TO_DATE function
STR_TO_DATE(entry_date, '%Y-%m-%d');
then using DATE_SUB function you can find last 30 days from current date
DATE_SUB(CURDATE(),INTERVAL 30 DAY)
So, your condition would be
STR_TO_DATE(entry_date, 'Y-m-d') < DATE_SUB(CURDATE(),INTERVAL 30 DAY)
Please see MySQL Date and Time Functions for further reference.
If you want to call a number that was not called in last 30 days then try.
last_local_call_time < DATE_SUB(NOW(), INTERVAL 30 DAY)
Related
Been trying to get this to work for 2 days and this is frustrating me.
Trying to get records 30 minutes before a date/time (Format in database is datetime).
This is what I have:
select id
from tbl_events
WHERE DATE_SUB(NOW(), INTERVAL -30 MINUTE) = DATE_FORMAT(start, '%Y-%m-%d %H:%i:%s')
What the heck am I missing?
Thanks
You already use the function DATE_SUB() so within that function you can simply use INTERVAL 30 MINUTE without the minus sign.
You also don't have to format start if it is a datetime or timestamp field.
Finally you shouldn't use = because times are hardly every exactly equal.
This gives this query:
select id
from tbl_events
WHERE start < DATE_SUB(NOW(), INTERVAL 30 MINUTE)
Probably. It's not extremely clear what you're trying to do.
Need help here, having an mysql table called APPROVAL, there having an id,dateandtime and level, i need a query that selects the id alone with the following condition.
Taking date alone from database and comparing it with current system date, if the days exceeds above 30 and below 60 and also level = 5.
How can I write a query for this.
Thanks in advance.
MySQL has good date arithmetic. For example, the expression
CURDATE() + INTERVAL 30 DAY
gives a datetime value denoting midnight 30 days hence. Similarly
CURDATE() + INTERVAL 61 DAY
yields midnight on the 61st day.
So a query of the form
SELECT ID
FROM APPROVAL
WHERE Level = 5
AND `DateTime` >= CURDATE() + INTERVAL 30 DAY
AND `DateTime` < CURDATE() + INTERVAL 61 DAY
will yield what you want. Notice the use of >= for the beginning of the range of days, and the use of < and an extra day for the end of the range. We do that because we want all items from the 60th day, and none from the 61st day.
A compound index on (Level, DateTime) will make this query very efficient to satisfy.
Notice that an expression like
DATE(`DateTime`) <= CURDATE() + INTERVAL 60 DAY /* slow! */
will also yield correct results, but the presence of the the DATE() function call on the column to be searched makes it unsargeable. That is, it makes MySQL unable to use an index to satisfy the search.
Ok so use this query to retrieve all the IDs that match level 5 and date diff between 30 and 60 compared to the current date.
SELECT id
FROM APPROVAL
WHERE level = 5 && DATEDIFF(NOW(), dateandtime) BETWEEN 30 AND 60
I'd suggest you to order them dy date DESC too.
Hope that helps
I hope, I understood your problem correctly.
select `ID`
from APPROVAL
where `Level` = 5
and ( DATE(`DateTime`) > curdate() + interval 30 day
and DATE(`DateTime`) < curdate() + interval 60 day )
order by `ID` asc;
Where DATE() gets the date from a datetime and CURDATE() is the current system date. With interval you can manipulate a date expression whitout having to worry about its limits.
I want to return records from database 15 days old from end _date to till end_date!
I am searching for the query for last 3 days!
However. I want your help to do a query. Its simple but I'm not sure how to do it.
I wrote query something like :
SELECT *
FROM bid_post
WHERE ending_date
BETWEEN DATE_SUB( DATE(`ending_date`) , INTERVAL 15
DAY )
AND ending_date >= CURDATE()
But it is not working !
The data column is a Varchar type. I am storing date as YYYY-mm-dd format
Does somebody now how can I accomplish this?
Thanks.
Please try with this query
SELECT *
FROM bid_post
WHERE ending_date between DATE_SUB( CURDATE() , INTERVAL 15 DAY )
AND CURDATE()
You should never store dates as varchar since these are not real dates and you need to store them using mysql native date data types.
Since the format is Y-m-d you may not need to do a conversion using str_to_date function and can use the query as
select * from bid_post
where
ending_date between date_sub(curdate(),interval 5 day) and curdate()
This will give you data from last 15 days till today.
Using conversion to real date you need str_to_date as
select * from bid_post
where
str_to_date(ending_date,'%Y-%m-%d') between date_sub(curdate(),interval 5 day) and curdate() ;
DEMO
I have a column in my database 'scheduledDate', I need all rows in the database that have a scheduledDate within 7 days from today.
I could use a query like this:
SELECT * FROM tblName WHERE `scheduledDate` > DATE_ADD(now(), INTERVAL 7 DAY
The only problem is, the 'scheduledDate' column is not formatted as a mysql timestamp (YYYY-MM-DD HH:MM:SS), but is formatted just as a standard American Date (07/09/2014). Is there a way to grab all rows within the next 7 days in my query? I was thinking that it might be possible using DATE_FORMAT, but I have been unable to figure it out.
You can convert the string to a date using str_to_date():
WHERE str_to_date(`scheduledDate`, '%m/%d/%Y') BETWEEN now() and DATE_ADD(now(), INTERVAL 7 DAY)
The logic also needs to change. The above uses between, but this may not be the logic your really need because of the extraneous time component on now(). Perhaps this is closer:
WHERE str_to_date(`scheduledDate`, '%m/%d/%Y') BETWEEN CURRENT_DATE() and DATE_ADD(CURRENT_DATE, INTERVAL 7 DAY)
WHERE scheduledDate >= Date_Add(now(), INTERVAL -7 DAY)
I have a datetime field (endTime) in mysql. I use gmdate() to populate this endTime field.
The value stored is something like 2009-09-17 04:10:48. I want to add 30 minutes to this endtime and compare it with current time. ie. the user is allowed to do a certain task only 30 minutes within his endTime. After 30 minutes of his endTime, they should not be allowed to do a task.
How can this be done in php?
I'm using gmdate to make sure there are no zone differences.
If you are using MySQL you can do it like this:
SELECT '2008-12-31 23:59:59' + INTERVAL 30 MINUTE;
For a pure PHP solution use strtotime
strtotime('+ 30 minute',$yourdate);
Try this one
DATE_ADD(datefield, INTERVAL 30 MINUTE)
MySQL has a function called ADDTIME for adding two times together - so you can do the whole thing in MySQL (provided you're using >= MySQL 4.1.3).
Something like (untested):
SELECT * FROM my_table WHERE ADDTIME(endTime + '0:30:00') < CONVERT_TZ(NOW(), ##global.time_zone, 'GMT')
Dominc has the right idea, but put the calculation on the other side of the expression.
SELECT * FROM my_table WHERE endTime < DATE_SUB(CONVERT_TZ(NOW(), ##global.time_zone, 'GMT'), INTERVAL 30 MINUTE)
This has the advantage that you're doing the 30 minute calculation once instead of on every row. That also means MySQL can use the index on that column. Both of thse give you a speedup.
Use DATE_ADD function
DATE_ADD(datecolumn, INTERVAL 30 MINUTE);
Get date from MySQL table by adding 30 mins
SELECT loginDate, date_add(loginDate,interval 30 minute) as newLoginDate
FROM `tableName`;
This will result like below
Login Date - 2020-07-22 14:00:00
New Login Date - 2020-07-22 14:30:00