Update date + one year in mysql - 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

Related

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)

MSQL, Query rows passed a certain date of an initial date?

How do i extract all rows greater then 7 days of a start date?, I'm trying to use this query in MySQL. Below is my statement.
SELECT * from v_polygons a
INNER JOIN tblProjectData z
on z.Project_ID = a.Project_ID
WHERE DATE_ADD(z.FlyDate, INTERVAL 7 DAY) > NOW() + INTERVAL rge DAY
I have a start date z.FlyDate, So i give it +7 days, then i check to see if that date is greater then NOW()
is this correct or have i messed it up?
You can just do:
WHERE DATE_ADD(DATE(z.FlyDate), INTERVAL 7 DAY) < DATE(NOW());
This will ignore the time part. You can remove DATE function call if you want to consider the time as well.

SQL DATE extraction from a column

I have a table 'schedule' and column 'travel_date'.
travel_date is having 'a predefined date' in that.
I want to alter that column with '5days' more.
like
UPDATE Schedule SET travel_date=''+5days ;
I used
UPDATE schedule SET travel_date = (travel_date+5);
It worked how ?
In MySQL you can do that with
UPDATE customer
SET register_date = DATE_ADD(register_date, INTERVAL 5 DAY)
Why would you want to add 5 days to every customers register date???
Are you sure this is what you want to do?
UPDATE customer SET [register_date] = DATE_ADD([register_date], INTERVAL 5 DAY)
If it is a datetime column, use the DATE_ADD() function:
UPDATE customer SET register_date = DATE_ADD(register_date, INTERVAL 5 DAY)
Using DATE_ADD()
You can use the DATE_ADD() function to handle adding a given interval (e.g. days, minutes, hours, etc.) to an existing date column:
UPDATE customer
SET register_date = DATE_ADD(register_date, INTERVAL 5 DAY)
Using Date Arithmetic
Alternatively, you can simply use date arithmetic as well, which is similar to your previous example:
UPDATE customer
SET register_date = register_date + INTERVAL 5 DAY

How to get first day of every corresponding month in mysql?

I want to get first day of every corresponding month of current year. For example, if user selects '2010-06-15', query demands to run from '2010-06-01' instead of '2010-06-15'.
Please help me how to calculate first day from selected date. Currently, I am trying to get desirable using following mysql select query:
Select
DAYOFMONTH(hrm_attendanceregister.Date) >=
DAYOFMONTH(
DATE_SUB('2010-07-17', INTERVAL - DAYOFMONTH('2010-07-17') + 1 DAY
)
FROM
hrm_attendanceregister;
Thanks
Is this what you are looking for:
select CAST(DATE_FORMAT(NOW() ,'%Y-%m-01') as DATE);
You can use the LAST_DAY function provided by MySQL to retrieve the last day of any month, that's easy:
SELECT LAST_DAY('2010-06-15');
Will return:
2010-06-30
Unfortunately, MySQL does not provide any FIRST_DAY function to retrieve the first day of a month (not sure why). But given the last day, you can add a day and subtract a month to get the first day. Thus you can define a custom function:
DELIMITER ;;
CREATE FUNCTION FIRST_DAY(day DATE)
RETURNS DATE DETERMINISTIC
BEGIN
RETURN ADDDATE(LAST_DAY(SUBDATE(day, INTERVAL 1 MONTH)), 1);
END;;
DELIMITER ;
That way:
SELECT FIRST_DAY('2010-06-15');
Will return:
2010-06-01
There is actually a straightforward solution since the first day of the month is simply today - (day_of_month_in_today - 1):
select now() - interval (day(now())-1) day
Contrast that with the other methods which are extremely roundabout and indirect.
Also, since we are not interested in the time component, curdate() is a better (and faster) function than now(). We can also take advantage of subdate()'s 2-arity overload since that is more performant than using interval. So a better solution is:
select subdate(curdate(), (day(curdate())-1))
This is old but this might be helpful for new human web crawlers XD
For the first day of the current month you can use:
SELECT LAST_DAY(NOW() - INTERVAL 1 MONTH) + INTERVAL 1 DAY;
You can use EXTRACT to get the date parts you want:
EXTRACT( YEAR_MONTH FROM DATE('2011-09-28') )
-- 201109
This works well for grouping.
You can use DATE_FORMAT() function in order to get the first day of any date field.
SELECT DATE_FORMAT(CURDATE(),'%Y-%m-01') as FIRST_DAY_CURRENT_MONTH
FROM dual;
Change Curdate() with any other Date field like:
SELECT DATE_FORMAT(purchase_date,'%Y-%m-01') AS FIRST_DAY_SALES_MONTH
FROM Company.Sales;
Then, using your own question:
SELECT *
FROM
hrm_attendanceregister
WHERE
hrm_attendanceregister.Date) >=
DATE_FORMAT(CURDATE(),'%Y-%m-01')
You can change CURDATE() with any other given date.
There are many ways to calculate the first day of a month, and the following are the performance in my computer (you may try this on your own computer)
And the winner is LAST_DAY(#D - interval 1 month) + interval 1 day
set #D=curdate();
select BENCHMARK(100000000, subdate(#D, (day(#D)-1))); -- 33 seconds
SELECT BENCHMARK(100000000, #D - INTERVAL (day(#D) - 1) DAY); -- 33 seconds
SELECT BENCHMARK(100000000, cast(DATE_FORMAT(#D, '%Y-%m-01') as date)); -- 29 seconds
SELECT BENCHMARK(100000000, LAST_DAY(#D - interval 1 month) + interval 1 day); -- 26 seconds
I'm surprised no one has proposed something akin to this (I do not know how performant it is):
CONCAT_WS('-', YEAR(CURDATE()), MONTH(CURDATE()), '1')
Additional date operations could be performed to remove formatting, if necessary
use date_format method and check just month & year
select * from table_name where date_format(date_column, "%Y-%m")="2010-06"
SELECT LAST_DAY(date) as last_date, DATE_FORMAT(date,'%Y-%m-01') AS fisrt_date FROM table_name
date=your column name
The solutions that use last_day() and then add/subtract a month and a day are not interchangeable.
Example:
date_sub(date_add(last_day(curdate()), interval 1 day), interval 3 month)
always works for any supplied number of months you want to go back
date_add(date_sub(last_day(now()), interval 3 month), interval 1 day)
will fail in some cases, for instance if your current month has 30 days and the month you're subtracting back to (and then adding a day) has 31.
date_add(subdate(curdate(), interval day(?) day), interval 1 day)
change the ? for the corresponding date
This works fine for me.
date(SUBDATE("Added Time", INTERVAL (day("Added Time") -1) day))
** replace "Added Time" with column name
Use Cases:
If you want to reset all date fields except Month and Year.
If you want to retain the column format as "date". (not as "text" or "number")
Slow (17s):
SELECT BENCHMARK(100000000, current_date - INTERVAL (day(current_date) - 1) DAY);
SELECT BENCHMARK(100000000, cast(DATE_FORMAT(current_date, '%Y-%m-01') as date));
If you don't need a date type this is faster:
Fast (6s):
SELECT BENCHMARK(100000000, DATE_FORMAT(CURDATE(), '%Y-%m-01'));
SELECT BENCHMARK(100000000, DATE_FORMAT(current_date, '%Y-%m-01'));
select big.* from
(select #date := '2010-06-15')var
straight_join
(select * from your_table where date_column >= concat(year(#date),'-',month(#date),'-01'))big;
This will not create a full table scan.