How can I UPDATE an expiration date in MySQL? - mysql

So, I need to reset the expiration dates for a bunch of coupon codes in our database. Our expirations dates are field "to_date" and are displayed as the following: to_date = '2013-04-14'
I need to set the to_date as 28 days after the from_date. So basically, something like this:
UPDATE salesrule
SET name = 'New coupon code', to_date = 'from_date + 28 days'
I know this would work for a simple int value, but I'm not sure how to do this give that the data displays as an actual date. I have no control over how the date itself displays, that's a built in Magento functionality.
I'm a big noob when it comes to MySQL, but I've done some research and I've found the format function: FORMAT(Now(),'YYYY-MM-DD') I have a feeling this may be the key... can someone point me in the right direction it terms of formatting or writing this command correctly? Thank you!

UPDATE salesrule
SET name = 'New coupon code', to_date = DATE_ADD(from_date, INTERVAL 28 DAY);
More info about the DATE_ADD() function here:
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add

to_date = DATE_ADD(from_date, INTERVAL 28 DAY)

Check this question out, it does what you want.
You can use the DATE_ADD() function:
... WHERE DATE(DATE_ADD(eventdate, INTERVAL -1 DAY)) = CURRENT_DATE
It can also be used in the SELECT statement:
SELECT DATE_ADD('2010-05-11', INTERVAL 1 DAY) AS Tomorrow;

Related

How to adjust timestamp to yesterday date on MySQL database?

Can anybody please tell me how to add a yesterday time stamp whenever my table get updated? Currently, it giving me today date instead yesterday date. Please see below picture.
I tired adding (CURRENT_TIMESTAMP, -1) on default/Expression. Did not work.
Comment Picture
Thank you so much
You can use SUBDATE(CURRENT_DATE, INTERVAL 1 DAY) to subtract one day from today's date. For example:
SELECT *
FROM table_name
WHERE DATE(Date) = SUBDATE(CURRENT_DATE(), INTERVAL 1 DAY)
UPDATE
To update the value in the database, you can do the following:
UPDATE table_name
SET date = SUBDATE(CURRENT_DATE(), INTERVAL 1 DAY)
WHERE column = 'value'
I also needed the same, to update yesterday date whenever my table gets data. I have scheduled the following query, which worked perfectly for me.
I hope it will work for you also:
sql_yesterday_date = 'UPDATE tbl_dailytrade SET Date = subdate(current_date, 1) WHERE Date(date) = CURDATE()'

UPDATE sql to change time only in datetime

I want to UPDATE time only from datetime using sql but it seem cant work. the update will get from user and only change the time.
example 2018-10-06 08:00:00 update to 2018-10-06 12:00:00 (time that user enter)
$sql3="UPDATE course
SET date_start = '$date_start'
WHERE date_start = SUBSTRING(date_start,11,17)
AND CourseNo = '$id1' ";
$hasil3=mysql_query($sql3);
if($hasil3==false)
echo "SQL error:".mysql_error();
else
{
?> <center>
<script language="javascript">
alert("Successfully update!");window.location="studentTimetable.php";
</script>
You can use an expression such as:
select date('2018-10-06 08:00:00') + interval 12 hour
You can also use addtime() if you want to add hours/minutes/seconds.
This should be simple enough to put into an update statement if that is what you want to do.
If I understand you right, you get an input as string in the format <hours> ":" <minutes> ":" <seconds>, that represents a time of the day. You want to replace the time of the day portion of a datetime column with that given time of the day.
To do so you can first downcast the column to a date and then upcast it again to a datetime. That way the time of the day portion becomes 00:00:00. Now use date_add to add the time of the day the user has given to you. Since the time of the day was zeroed before that will result in a datetime with the time of the day as the user put in.
UPDATE elbat
SET nmuloc = date_add(cast(cast(nmuloc AS date) AS datetime), INTERVAL '12:00:00' HOUR_SECOND);
I had a similar problem to this..
I have a number of dates that are not quite right by a few seconds. They should all end on an Hour, i.e 00:00
Work out how much time I need add (I know i want to add some seconds)
SELECT 60 - DATEPART(SECOND, EndDate), e.*
FROM Events e
WHERE DatePart(SECOND, EndDate) <> 0
Now I can write an update to correct all the dates that are slightly off.
UPDATE Events
SET EndDate = DATEADD(SECOND, i.Seconds, i.EndDate)
FROM (
SELECT Id, 60 - DATEPART(SECOND, EndDate) AS Seconds, EndDate
FROM Events e
WHERE DatePart(SECOND, EndDate) <> 0
) i
WHERE
i.ID = Events.ID

Performing date arithmetic while updating table value

I tried every possible bracket combination but I just can't get this line to work!
I also tried looking for the solution but I can't seem to find anything similar. I hope someone can shed some light on my ignorance ^^.
UPDATE contact_info SET birthday= CURDATE() - TIME_TO_SEC(NOW()) % 50000 WHERE contactID=1;
Thanks in advance!
You need to convert the expression you calculate to an INTERVAL specifying the number of days. Then use DATE_SUB to subtract the interval from the current date.
UPDATE contact_info
SET birthday = DATE_SUB(CURDATE(), INTERVAL (TIME_TO_SEC(NOW()) % 50000) DAY)
WHERE contactID = 1;

How can I get the yesterday's data and show it to my form today?

The idea is that I have this 'Present' form that is a textbox one that the user can input the data.
Now, I want to show yesterday's data(readonly) for comparing the result yesterday vs the result today.
Ex:
Attendance: (yesterday- readonly textbox)
Andy Shrob
Paula Guinto
Mylene Miles
Attendance (TODAY)
Paula Guinto
Mylene Miles
How can I get yesterday and show it so I can see who's present yesterday and who's present today? I'm coding PHP and Javascript
I was thinking of this:
mysql_query("Select prestoday, presyesterday
FROM attendance
WHERE ???)
All ideas are welcome. ;) Whether it be PHP code or Javascript or MYSQL Query
TABLE STRUCTURE:
mysql_select_db("csdcon", $con);
$sql="INSERT INTO attendance (prestoday, presyesterday, userdateinp, yesterdate)
VALUES
('$_POST[prestoday]','$_POST[presyesterday]','$_POST[userdateinp]','$_POST[yesterdate]')";
mysql_query("Select prestoday, presyesterday
FROM attendance
WHERE DATE(`datetimefield`) = DATE(DATE_SUB(NOW(), INTERVAL 1 DAY))
Mysql query
SELECT DATE_SUB(NOW(), INTERVAL 1 DAY);
will return yesterday date...
Hope it helps.
TO get yesterday date
SELECT DATE_SUB(date_time,INTERVAL 1 DAY)
To get yesterday results randomly
SELECT * from test_table where date_time = DATE_SUB(date_time,INTERVAL 1 DAY) order by rand()
You can get Date From JavaScript as
var today= new Date();
var yesterday = new Date();
yesterday .setDate(today.getDate()-1);
alert('today'+today);
alert('yesterday '+yesterday );
Use Date related functions in the WHERE condition:
e.g
WHERE ??? = DATE_SUB(NOW(), INTERVAL 1 DAY)
For further reference, see here.

Recordset for todays date using CURRENT_TIMESTAMP

hopefully this is an easy one.
I have a query that I want to produce results for todays date only based on a column (record_date) that uses CURRENT_TIMESTAMP.
so my query goes...
Select columns FROM fields WHERE table.record_date = DATE_SUB(NOW());
This is throwing up an error... :(
Thanks for you help....So i tried....
SELECT * FROM daily_record WHERE record_date = CURDATE()
but it yielded no result.
Here is a sample of the data in the column i am searching...
2011-03-31 11:28:37,
2011-03-31 11:28:37,
2011-03-31 11:28:37,
.....
Does it matter that the time is also saved?
Is that what you want ?
Select columns FROM fields WHERE table.record_date > CURDATE();
DATE_SUB() is for subtracting an interval from a date in MySQL. You've got DATE_SUB(now()), but don't specify an interval
It should be something like
... DATE_SUB(now(), INTERVAL 5 DAY);
so MySQL's complaining about the unexpected ), because of the missing interval.
If you want to convert 'now' into a date, you can simply use CURDATE(), or DATE(now())