Adding days to date column and result in new column? - mysql

I need to add 40 days to a column , which contains date in d-m-y format,
and insert result into new column . eg
$querydate="UPDATE services SET paymentdue_date=payment_date+30 DAY WHERE my condition ";
and i tried
$querydate="SELECT up_id=".$up_id.",DATE_ADD(payment_date,INTERVAL 30 DAY) AS paymentdue_date FROM up_services";
Column structure for payment_date and paymentdue_date is varchar10 utf8_unicode_ci
any suggestions?

correct query will be
$querydate="SELECT up_id,DATE_ADD(payment_date,INTERVAL 30 DAY) AS paymentdue_date FROM up_services where up_id='$up_id'";
you can't add compare in select like up_id=".$up_id." use where instead
if you want update query you can follow #saharsh shah answer

Try this:
UPDATE services
SET paymentdue_date = DATE_ADD(STR_TO_DATE(payment_date, '%e-%c-%y'), INTERVAL 30 DAY)
WHERE my condition

I tried running this query and it worked for me , updated my column with new :)
$querydate="UPDATE up_services SET paymentdue_date=DATE_ADD(payment_date,INTERVAL 30 DAY) WHERE my condition ";

Related

mysql edit time only from date [duplicate]

How can I update only the time in an already existing DateTime field in MySQL? I want the date to stay the same.
Try this:
UPDATE yourtable
SET yourcolumn = concat(date(yourcolumn), ' 21:00:00')
WHERE Id = yourid;
Try this:
UPDATE t1 SET DateTimeField = CONCAT(DATE(DateTimeField),' 12:34:56');
UPDATE myTable
SET myDateTime = ADDTIME(DATE(myDateTime), #myTimeSpan)
WHERE id = #id;
Documented on MySQl date functions MySQL docs
I have solved in this way:
UPDATE table
SET myDateTime = CONCAT_WS(' ',DATE(myDateTime), CURTIME())
WHERE id = #id;
Obviously you should change CURTIME() with your desired time.
UPDATE myTable
SET myDateTime = ADDTIME(myDateTime, #myTimeSpan)
WHERE id = #id;
For exact syntax of function, see this.
Try this:
UPDATE sms
SET entry_period_end_date= entry_period_end_date+INTERVAL 6 Hour
WHERE TIME(entry_period_end_date) = '06:00:00';
UPDATE `table`
SET time = ADDTIME(time, INTERVAL 13 Hour);
Well, exactly what you are asking for is not possible. The date and time components can't be updated separately, so you have to calculate the new DateTime value from the existing one so that you can replace the whole value.
MySQL DEV page shows functions like subtime and difftime
A sample code to back the time all posts in 3 hours is above:
UPDATE tablepost SET datepost = SUBTIME( datepost , '0 3:0:0' );
Note that values 0 dont alter the respective field. Take care this code, use select first to test these function.
Reference: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_subtime
Asuming you have a DATE field and TIME field and want to inject the time into the date, try this:
UPDATE mytable
SET mydatefield = ADDTIME( DATE_FORMAT(mydatefield,'%Y-%m-%d 00:00:00'), mydatefield)
WHERE myid = ...
I used ADDTIME in the following way
Earlier in my cloud server, the DateTime was set to UTC but after changing the DateTime to Asia/Kolkata ie UTC 5:30 I wanted the same to reflect in my database tables.
I wanted to update the created_at and updated_at column by 5 hours 30 minutes. I did the following
To update all the rows of the table
UPDATE
products
SET
created_at = ADDTIME(created_at, '5:30:0'),
updated_at = ADDTIME(updated_at, '5:30:0')
You can omit the WHERE condition if you want to update all the records, but since my new records were updated with proper values. So only my rows below id less than 2500 must be updated
UPDATE
products
SET
created_at = ADDTIME(created_at, '5:30:0'),
updated_at = ADDTIME(updated_at, '5:30:0')
WHERE
id < 2500;
This what helped me. I convert time to minutes firstly: 150 for 2:30 am.
UPDATE lesson SET starts_at = DATE_ADD(Date(starts_at), INTERVAL 150 MINUTE)
Minutes are enough accurate for me, though you can use other units: https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-add

Inserting time in mysql database

Actually I have time and exp field in mysql database.
I am successfully inserting current time in time field using : CURTIME();
But i also want to insert exp time in exp filed ie: current time + 2 hrs..
It should show :
time : 4:00:12AM
exp : 6:00:12AM
You could use addtime() for this.
So for your example:
SELECT ADDTIME(curtime()', '02:00:00.0000')
should work.
You can try this:-
SELECT ADDTIME(CURTIME(),'02:00:00.000000');
Hope This Will Help You.
Use DATE_ADD function to specify the interval which is 2 hours in your case. DATE_ADD(CUR_TIME, INTERVAL 2 HOUR) will work fine.
Here is the great documentation for date time functions -
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
Use following format:
INSERT INTO table1
(field1, field2,....,cur_time_field,exp_field,...fieldN)
VALUES
(value1,value2,....,NOW(),DATE_ADD(NOW(), INTERVAL 2 HOUR),...fieldN)
Update:
INSERT INTO table1
(field1, field2,....,cur_time_field,exp_field,...fieldN)
VALUES
(value1,value2,....,NOW(),DATE_ADD(CURTIME(), INTERVAL 2 HOUR),...fieldN)

How to update timestamp column moving date forward

what I am looking for is some help with a query.
I have a MySql field with unixtime in it representing a date in each of the next few dozen months. I have to move the dates forward to the first day of the next month for each entry in the table.
The dates are all the 20th of each month, and so I want to move June 20 to July 1, July 20 to August 1, and so on. I can't just add 11 days, because that wouldn't be the first day of the next month when considering months with 31 days and February.
I have been playing with ideas like this:
update table set column = UNIX_TIMESTAMP(column + MONTH(column)+1,DAY(1)) where index_column = '1234'
but I am pretty sure that won't work. I could use something like this to convert it, then try to convert it back:
update table set column = DATEFORMAT(column,'%Y-$c-%d %H:%i:%s') where index_column == '1234'
I still think there has to be a better way. Frankly, I would update the few dozen manually, but I know this will come up frequently, and don't want to have to do it manually every time.
I prefer not to use code, but would instead like to just do it directly into MySql. I hope there is someone out there that can help me figure this out.
Thank you in advance.
Maybe something like this Works:
update table set column = UNIX_TIMESTAMP(LAST_DAY(FROM_UNIXTIME(column)) + INTERVAL 1 DAY) where index_column = '1234'
Reference: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_last-day
Does ADDDATE do it for you? Or do you need something more?

MYSQL Inner Join & Get value from Subquery

OK, I am trying to compare two tables and then input a list from a third of names to produce a totals of values for the prior 10 days. The query runs but gives me a NULL result and only one result. If I remove the DATE_ADD and replace it with a real date from the database and put in a value for the left clause instead of using the subquery I do get what I am looking for that specific date. What I would like to do is create a list of the names with the values for that day and the last 10 days. I am sure I am not doing this right so any help is appreciated as I am fairly new to this. Simple queries are easy but putting something complex like this is new to me.
select sum(t.price) from td.trs as t
inner join td.order as o on o.trsid=t.id
inner join pts.product as p on p.id=o.grp_id
where t.invoice_date=DATE_ADD(CURRENT_DATE(),INTERVAL 10 DAY)
and left(t.mfgid,3) IN (select name from name.list);
change
where t.invoice_date=DATE_ADD(CURRENT_DATE(),INTERVAL 10 DAY)
to
where t.invoice_date >= DATE_ADD(CURRENT_DATE(),INTERVAL -10 DAY)
You are probably not getting any results because you are doing DATE_ADD to the CURRENT_DATE(), which will give you a date 10 days in the future. If you want to get all items for last 10 days, use
WHERE t.invoice_date BETWEEN CURRENT_DATE() AND DATE_SUB(CURRENT_DATE(), INTERVAL 10 DAY)

MySQL Return Today's Records Using DATE from DATETIME Field

I have a table called "actions" with a DATETIME column called "occurred". I'm trying to return the records for today by doing the following
SELECT * FROM `actions` WHERE `occurred` = DATE(NOW());
But I get an empty result set. If I take the WHERE clause out, I can see all 295 rows in the table and there's at least 30 rows from today. Later I will be writing another query to return all records between today's date and X amount of days in the past but before I can get there I need to know why this query is returning an empty result set.
Thanks in advance.
SELECT * FROM actions WHERE DATE(ocurred) = CURDATE();
DATE(ocurred) ignores the time part.
Here's the SQL Fiddle to play with the data: http://www.sqlfiddle.com/#!2/81708/2
If there in no future date in occurred, you could just use below:
SELECT * FROM `actions` WHERE `occurred` > DATE_SUB(CURDATE(), INTERVAL 1 DAY);