UPDATE sql to change time only in datetime - mysql

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

Related

Get users that have payment due in one day sql

This is probably easy to do but I can't seem to get my head around it. I have a user table that has a next_payment DATETIME column which gets update every month. I would like a query to get all the users where their next_payment DATETIME is in one day from the current datetime.
I tried something like this but it also gets me users where their next_payment is due in like 15 minutes. Not good
SELECT * FROM users WHERE next_payment >= NOW() AND next_payment <= NOW() + INTERVAL 1 DAY
I also tried something like this but this doesn't work either as it gives me all users that had next_payment datetime like 2 or 3 months ago (Not good).
SELECT * FROM users WHERE next_payment <= NOW() + INTERVAL 1 DAY
Thanks in advance for any help
Need to Cast the datetime field to date so you can include all data within the period and use between
SELECT * FROM
users
WHERE
cast(next_payment as date)
between cast(NOW() as date)
AND cast(NOW() + INTERVAL 1 DAY as date)
DATEDIFF works fine in MySQL:
SELECT
*
FROM
users
WHERE
DATEDIFF(next_payment, NOW()) = 1
SQL Fiddle for this example.
I would use:
SELECT * FROM users
WHERE TIMESTAMPDIFF(HOUR,NOW(),next_payment) <= 24

mysql retrieve difference between current and last hour

For the below table, i would like to get the difference between last hour and current hour for col-D and col-E for each of the site.
As part of that I am trying to first get the latest (current) hour entries for each of the site, but the following query is only listing me the entries with endTime as 01:00:00, when i have entries upto 9.00AM
select distinct(mmeName), endDate, endTime, c_ratio, set_time from attach where
type='INIT' and Date(endDate)=curDate() and
Time(endTime) >= DATE_ADD(Time(endTime), INTERVAL -1 HOUR) group by mmeName;
Any help would be appreciated for the immediate issue and as well finding the difference between current and last hour.
EDITED
I think this is what you are looking for. This will give you any records where the endTime is one hour prior to the latest current time for each mmeName. The 'max' sub-select gets the latest end datetime for each mmeName, and the join back matches on record exactly one hour prior to that.
SELECT mmeName, endDate, endTime, c_ratio, set_time
FROM attach a
JOIN
(SELECT mmeName, CONCAT(endDate, ' ' , endTime) max_endDateTime
FROM attach
WHERE type = 'INIT'
ORDER BY endDate DESC, endTime DESC
) AS max ON max.mmeName = a.mmeName
AND max.max_endDateTime = DATE_ADD(CONCAT(endDate, ' ' , endTime), INTERVAL 1 HOUR)
WHERE type = 'INIT'
;
ORIGINAL
select mmeName, endDate, endTime, c_ratio, set_time
from attach
where type='INIT' and Date(endDate)=curDate() and
endTime >= DATE_SUB(now(), INTERVAL -1 HOUR)
group by mmeName;
Note: If there are multiple matching records for a given mmeName, this query will just grab one of them.
EDITED: You need drop the TIME() functions from the WHERE clause. Both would have the date and time and if you didn't, if you ran it between 12:00 AM to 1:00 AM it would not return any results.

How can I UPDATE an expiration date in 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;

SELECT all records that are 30 days old

I need to SELECT all records that are 30 days old. I have the code below but it's not working. In updatestatus I have dates like 12/26/2011. I create a 30 day old date like
$onemonthago="01/01/2012";
$sth = $dbh->prepare(qq(
SELECT *
FROM people
WHERE STR_TO_DATE (updatestatus,'%m/%d/%y')
<= STR_TO_DATE ( "$onemonthago",'%m/%d/%Y')
) );
If the datatype of updatestatus is date:
SELECT *
FROM people
WHERE updatestatus <= '2012-01-01'
or:
SELECT *
FROM people
WHERE updatestatus <= CURRENT_DATE() - INTERVAL 1 MONTH
If the datatype is datetime or timestamp and you want to check the time part, too:
SELECT *
FROM people
WHERE updatestatus <= NOW() - INTERVAL 1 MONTH
You can put an exact datetime instead of the NOW() - INTERVAL 1 MONTH. The correct way depends on how you are storing the datetimes or timestamps (does the Perl code or MySQL creates them in the first place?).
You could also put - INTERVAL 30 DAY which yield slightly different results.
This is what I used. Very simple
$sth = $dbh->prepare(qq(SELECT * FROM people WHERE updatestatus + INTERVAL 30 DAY <= NOW() )) or die $DBI::errstr;
If the time column is in timestamp then use below query.(use from_unixtime function)
SELECT wd.* FROM `watchdog` as wd
WHERE from_unixtime(wd.timestamp) <= NOW() - INTERVAL 1 MONTH
You can try this way. In SQL, there is dateadd function and I think there should be similar function in MySQL.
select *
from Table
where str_to_date between dateadd(day,-30,getdate()) and getdate()
It retrieve records between current date and past 30 days. You need to adjust for time. If you don't count time, you need to remove timestamp.

finding events from previous Sunday to next Sunday

How can you find MySQL data for the current week plus the following Sunday?
Given a date (e.g. Wednesday 5/18/11), it would show events from the previous Sunday to the next Sunday. 5/15/11 through 5/22/11.
The trick would be to find the 'previous' Sunday to a given date.
How can this be done?
SELECT *
FROM events
WHERE Yearweek(`eventdate`) = Yearweek(NOW())
OR ( Weekday(NOW()) = 6
AND Yearweek(`eventdate`) = Yearweek(
DATE_SUB(NOW(), INTERVAL 1 DAY)) )
Taking from Pentium's answer, with some adjustments...
SELECT
*
FROM
Events
WHERE
YEARWEEK(`eventdate`) = YEARWEEK(NOW()) OR
(
WEEKDAY(`eventdate`) = 6 AND
YEARWEEK(`eventdate`) = YEARWEEK(NOW()) + 1
)
This may need to be adjusted depending on the values for WEEKDAY (is 6 Sunday?).
Also, while this should work, my guess is that mySQL won't be able to use any indexes on the eventdate column with this method. It's probably better to find the actual dates themselves for the bordering Sundays and then do a BETWEEN or <= >=. This should allow the use of an index on the eventdate. Even if you don't have an index on it now, you might want to use one in the future.
Using a calendar table . . .
select cal_date
from calendar
where cal_date between
(select max(cal_date) from calendar
where cal_date <= '2011-05-15' and day_of_week = 'Sun') and
(select min(cal_date) from calendar
where cal_date > '2011-05-15' and day_of_week = 'Sun')
It's not clear what you want if the given date is a Sunday. This previous query returns 15 rows given a date that falls on Sunday. It returns 8 rows for all other days. You can tweak the comparison operators in the WHERE clause to get the behavior you want.
I posted code for a calendar table earlier on SO. It's for PostgreSQL, but you should be able to adapt it to MySQL without much trouble.