MySQL Add month and modify day in update query - mysql

I need to modify several dates in a table using a mysql update statement.
Here is the problem: I need to modify each date adding 1 month and setting the day to 7.
Rule: +1 month and day needs to be 7
2015-10-10 => 2015-11-07
2015-11-05 => 2015-12-07
2015-12-21 => 2016-01-07
....
It's pretty easy do add a month, i've already tried using DATE_ADD(eventdate, INTERVAL 1 MONTH) but i need to change the day as well.
Started building the following query:
UPDATE receipts SET due_date = DATE_ADD(due_date, INTERVAL 1 MONTH) WHERE ...
Now i need to change the day.
How to do that? Thanks

You can try this (don't forget to add your WHERE clause):
UPDATE receipts
SET due_date = CONCAT(DATE_FORMAT(DATE_ADD(due_date, INTERVAL 1 MONTH), '%Y-%m-'), '07')

Related

How to get records that are less than n minutes?

I am trying to get the records from php that every row have following table:
[created_at] => 2022-10-15 08:17:52
I want get record that are created last minute.
for that i my php file:
SELECT * FROM customer_billing_details WHERE DATE_ADD(created_at, INTERVAL 1 MINUTE) >= NOW();
but it returning the same row even after it is more than 1 minute old,
I dont able to understand what is happening.
You do not need >= NOW(). the interval will automatically handle the time interval. use something like
SELECT *
FROM `customer_billing_details`
WHERE `created_at` >= CURRENT_TIMESTAMP - INTERVAL 5 MINUTE;
Change 5 minutes to 1 or 2 according to your needs.

How to update timestamp

I'm now having trouble with using dateadd() function. Basically I want to add 1 day to cexpireday(timestamp) if it is at least 10 days after current day.
I have tried two ways but neither of them work
update card
set cexpireday = dateadd(day,1,cexpireday)
where cexpireday - current_timestamp() >= '0000-00-10 00:00:00'
and
update card
set cexpireday = date_add(cexpireday,INTERVAL 10 day )
where datediff(day,cexpireday,current_timestamp) >= 10
I got "dateadd does not exist" for the first and "incorrect parameter to navigate function datediff()" for the second.
Can anyone help me with this?
Maybe try this:
UPDATE card
SET cexpireday = DATE_ADD(cexpireday, INTERVAL 1 DAY)
WHERE cexpireday > DATE_ADD(NOW(), INTERVAL 10 DAY)
You need to use DATE_ADD instead of dateadd.
Also you need to have a look at documentation.
This is example how to use it DATE_ADD(date, INTERVAL 10 DAY).
Also DATEDIFF expects 2 params start_date and end_date and returns number of days between 2 dates.

PHP to SQL add days to date column and post it to new date column

I to have this
Date
29/03/2018
+2
Due
31/03/2018
I am using this code , very simple and it works if i add seconds and hours, but if i go above 1 day the due column goes to 00:00:000
$sq2= "UPDATE Tickets SET Tickets.due= date(`date`)+20000;
( this currently adds 2 hours..)
how do i get past 1 day and have it work correctly?
If you want a day you can use date_add
UPDATE Tickets
SET Tickets.due= DATE(DATE_ADD(`date`, INTERVAL 1 DAY))
if you need minute hour and minute too the don't use date but
UPDATE Tickets
SET Tickets.due= DATE_ADD(`date`, INTERVAL 1 DAY)

Query results on every 6 months (display results 7 days before)

I am trying to make an app for the "car maintenance shop".
In MySQL table I have
ID
CAR_PLATE
MAINTENANCE_DATE (timestamp)
Every car that goes to the maintenance, must come again after every 6 months for a new check.
When the car arrives for the first time, the date will be timestamped in field "maintenance_date".
Question is...
How to display which car (license plate) is (let's say 7 days) near the period of the new check (maintenance)?
example
CAR_PLATE = XYXYXY,
MAINTENANCE_DATE = 1.1.2018
At 25.6 I should get a car plate same as the date (d/m) of the new maintenance (exactly 6 months after) and it would be displayed until maintenance day passes.
P.S.
I tried with "maintenance_date > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 7 DAY)";" but I am not on a good way..
The following code will be helpful for you.
SELECT CAR_PLATE
FROM table_name
WHERE DATE_ADD(MAINTENANCE_DATE, INTERVAL 6 MONTH)
BETWEEN CURDATE() AND (DATE_ADD( CURDATE(), INTERVAL 7 DAY));
If you are taking time for your comparison simply replace NOW() instead of CURDATE().Check the code given below:
SELECT CAR_PLATE
FROM table_name
WHERE DATE_ADD(MAINTENANCE_DATE, INTERVAL 6 MONTH)
BETWEEN NOW() AND (DATE_ADD( NOW(), INTERVAL 7 DAY));
I have attached my SQLfiddle with this. Thank you!
Try the following, simple approach to select records where the difference in days is between 0 and 7 (in the future).
SELECT ID, CAR_PLATE, MAINTENANCE_DATE
FROM <Table>
WHERE TIMESTAMPDIFF(DAY, NOW(), MAINTENANCE_DATE) BETWEEN 0 AND 7
;

Update date + one year in mysql

When I want setting numerical value +1 in mysql table, I use e.g.:
UPDATE table SET number=number+1 WHEN ...
How can I set date + one year?
Thanks
You could use DATE_ADD : (or ADDDATE with INTERVAL)
UPDATE table SET date = DATE_ADD(date, INTERVAL 1 YEAR)
This post helped me today, but I had to experiment to do what I needed. Here is what I found.
Should you want to add more complex time periods, for example 1 year and 15 days, you can use
UPDATE tablename SET datefieldname = curdate() + INTERVAL 15 DAY + INTERVAL 1 YEAR;
I found that using DATE_ADD doesn't allow for adding more than one interval. And there is no YEAR_DAYS interval keyword, though there are others that combine time periods. If you are adding times, use now() rather than curdate().
For multiple interval types use a nested construction as in:
UPDATE table SET date = DATE_ADD(DATE_ADD(date, INTERVAL 1 YEAR), INTERVAL 1 DAY)
For updating a given date in the column date to 1 year + 1 day