SQL Date_Sub syntax error - mysql

I have been looking at this statement for ages and simply cannot find the error can you guys help?
SELECT XD.*, UhED.row_class,
(SELECT id
FROM Comment C
WHERE C.Excel_Data_Excel_Lists_id = XD.Excel_Lists_id
AND C.Excel_Data_row = XD.row
LIMIT 1
) AS has_activity
FROM User_has_Excel_Lists UhXL
JOIN Excel_Lists XL
ON XL.id = UhXL.Excel_Lists_id
JOIN Excel_Data XD
ON XD.Excel_Lists_id = XL.id
LEFT JOIN User_has_Excel_Data UhED
ON UhED.Excel_Data_Excel_Lists_id = XL.id
AND UhED.Excel_Data_row = XD.row
AND UhED.User_id = 1
WHERE UhXL.User_id = 1
AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2)<-- it says that the error is here
GROUP BY XD.telephone
ORDER BY last_name ASC, first_name ASC

AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2 DAY)
try this it may help you

You forgot do specify the interval unit maybe? Something like INTERVAL 2 DAY or INTERVAL 2 HOUR maybe?

You may need to specify the measure for the interval (e.g., days, months, etc.)
You can check the syntax for DATE_SUB here

if i understand your problem like follow this instructions as:
you should must mention the day, month, hour, or year in the following line:
your code:
AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2)
change code: DAY
AND XL.created > DATE_SUB(DATE(now()), INTERVAL 2 DAY)
if you are using the DATE(tablename) in your table you getting error like:
WRONG CODE (sample)(http://sqlfiddle.com/#!2/5b7e2/7):check the code no:5
SELECT id,DATE_SUB(DAT(NOW()), INTERVAL 2 DAY)
FROM supportContacts;
RIGHT CODE:
SELECT id,DATE_SUB(DATE(NOW()), INTERVAL 2 DAY)
FROM supportContacts;
REFER THIS LINK: http://sqlfiddle.com/#!2/5b7e2/7

Related

Declaring and populating Attributes on a table MySQL [duplicate]

I have a table in MySQL. What would be the sql statement look like to add say 2 days to the current date value in the table?
UPDATE classes
SET
date = date + 1
where id = 161
this adds one second to the value, i don't want to update the time, i want to add two days?
Assuming your field is a date type (or similar):
SELECT DATE_ADD(`your_field_name`, INTERVAL 2 DAY)
FROM `table_name`;
With the example you've provided it could look like this:
UPDATE classes
SET `date` = DATE_ADD(`date` , INTERVAL 2 DAY)
WHERE `id` = 161;
This approach works with datetime , too.
UPDATE table SET nameofdatefield = ADDDATE(nameofdatefield, 2) WHERE ...
This query stands good for fetching the values between current date and its next 3 dates
SELECT * FROM tableName
WHERE columName BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 3 DAY)
This will eventually add extra 3 days of buffer to the current date.
You can leave date_add function.
UPDATE `table`
SET `yourdatefield` = `yourdatefield` + INTERVAL 2 DAY
WHERE ...
update tablename set coldate=DATE_ADD(coldate, INTERVAL 2 DAY)
For your need:
UPDATE classes
SET `date` = DATE_ADD(`date`, INTERVAL 2 DAY)
WHERE id = 161
DATE_ADD(FROM_DATE_HERE, INTERVAL INTERVAL_TIME_HERE DAY)
will give the Date after adjusting the INTERVAL
eg.
DATE_ADD(NOW(), INTERVAL -1 DAY) for deducting 1 DAY from current Day
DATE_ADD(NOW(), INTERVAL 2 DAY) for adding 2 Days
You can use like
UPDATE classes WHERE date=(DATE_ADD(date, INTERVAL 1 DAY)) WHERE id=161
SELECT DATE_ADD(CURDATE(), INTERVAL 2 DAY)
SET date = DATE_ADD( fieldname, INTERVAL 2 DAY )
SELECT ADDDATE(d,INTERVAL 1 DAY)
from table

Mysql Add DATE_COLUMN 3 DAYS with query

I've been asking many question here, but it was dead end, I just want to make a simple select from date_column+3 day, I'm new to MySQL.
I've tried with this query
SELECT *
FROM pemesanan
WHERE pemesanan.`date` = DATE(DATE_ADD(date, INTERVAL 3 DAY))
but the result is empty, here's my column
date
2016-08-10
2016-08-04
2016-08-07
it must be show the 2016-08-10 data, but its not,
Can somebody enlighten me, I've been frustrated with this
If you use date right side you should use date left side
SELECT *
from pemesanan
where date(pemesanan.`date`) = DATE(DATE_ADD(NOW(), INTERVAL 3 DAY))
If you use the same date pemesanan.date you never get pemesanan.date = pemesanan.date +3
but if you want select the date older than 3 day you shuold use
SELECT *
from pemesanan
where date(pemesanan.`date`) <= DATE(DATE_sub(NOW(), INTERVAL 3 DAY))
and for delete
DELETE from pemesanan
where date(pemesanan.`date`) <= DATE(DATE_sub(NOW(), INTERVAL 3 DAY))
in your case
SELECT *
from pemesanan
where date(pemesanan.tanggal) <= DATE(DATE_sub(NOW(), INTERVAL 3 DAY))

Records between NOW() and 3am today or yesterday

How can I get records in a table between NOW() and the previous 3am?
This would be easy if it's 9am, but how do I write this if it's 2am? i.e I want the trades between DATE_SUB(CURDATE(), INTERVAL 21 HOURS) and NOW(). I'm looking for some code which can do both without needing to check the time in usercode and choose between two sql statements.
I'm sure there's a simple solution to this, but it's eluding me.
A simple idea is to subtract three hours and compare the date:
where date(date_sub(col, interval 3 hour)) = (case when hour(date) >= 3 then curdate() else date_sub(curdate(), interval 1 day)
Or, more explicitly, just do the comparison in SQL:
where (hour(date) >= 3 and date(col) = curdate()) or
(hour(date) < 3 and date(col) = date_sub(curdate(), interval 1 day)
I'll add my own answer (I finally gave up and explored using IF and found it could be used inside SELECT statements), but #Gordon Linoff might be better - I've no idea which of these is faster.
SELECT IF (NOW() > DATE_ADD(CURDATE(), INTERVAL 3 HOUR),
DATE_ADD(CURDATE(), INTERVAL 3 HOUR),
DATE_SUB(CURDATE(), INTERVAL 21 HOUR))
which can then be used as the conditional on an outer SELECT.

How to add date condition to my query?

I have this SQL statement. It works, and I need to add another one condition.
I need to sort it by date. occurence - is my date row.
SELECT dd.caption, COUNT(t.occurence)
FROM transaction t
INNER JOIN dict_departments dd
ON dd.id = t.terminal_id
GROUP BY dd.caption
How to add this condition:
WHERE t.occurence BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH)
to my query.
Try this:
WHERE t.occurrece BETWEEN current_date() AND dateadd(month,1,current_date())
The function dateadd is a SQL SERVER function, but the rest of the clause is standard SQL.
BETWEEN requires two arguments, a start point and an end point. If your end point is the current time, you have two options:
Using BETWEEN:
WHERE t.occurence BETWEEN (CURRENT_DATE() - INTERVAL 1 MONTH) AND NOW()
Using simple comparison operator:
WHERE t.occurence >= (CURRENT_DATE() - INTERVAL 1 MONTH)
If you want to filter dates from 1 month ago till now:
WHERE (t.occurrece BETWEEN DATE_ADD(CURDATE(), INTERVAL -1 MONTH) AND CURDATE()) = 1
or
WHERE (t.occurrece BETWEEN ADDATE(CURDATE(), INTERVAL -1 MONTH) AND CURDATE()) = 1

How Do I Add 2 Hours from Formatted Time

I need to know how to add 2 hours to the below 'Completed' timestamp.
Here is the Select statement
Select Tsk.task_id,Tsk.org_id,Tsk.completed,Tsk.assgn_acct_id,name
FROM tdstelecom.tasks As Tsk
WHERE Tsk.task_id = '11094836'
AND DATE(Tsk.completed) < CURDATE() AND DATE(Tsk.completed) >= DATE_SUB(CURDATE
(),INTERVAL 180 DAY)
Here are the results: 2012-08-22 14:18:14
Desired results: 2012-08-22 16:18:14
Your tag says mySQL, use the subtime(exp1,exp2) function SUBTIME(Tsk.completed, '02:00:00.000000') in your select should do the trick.
select Tsk.completed+interval 2 hour,Tsk.assgn_acct_id,name
FROM tdstelecom.tasks As Tsk
WHERE Tsk.task_id = '11094836'
AND DATE(Tsk.completed) < CURDATE() AND DATE(Tsk.completed) >= DATE_SUB(CURDATE
(),INTERVAL 180 DAY)