I need to change the server date via sql.
Preferably through my application (CL is also good).
How do I change the mysql server date using SQL?
Why:
We test a scenario that spans several weeks. To test it we need to advance the server date by that time.
Do you want to change the server date or the server timezone?
If it is the server date, you have to do it at the OS level.
If it is the time zone, you can change it globally for entire database instance or for your session/connection only.
The SET TIMESTAMP statement affects the value returned by NOW() but not by SYSDATE()
Related
Good day good sirs.
I have a winform application and a Mysql server for the database. In my application, I have several date fields wherein it displays dates of transactions like start date and time. In the application, it is set to follow the date and time of the computer (client) and not the server and when I change the client date and time, it saves as it is and not the server time which compromises data integrity and accuracy. How can i set the client machine to follow the server date and time. Is there any way or techniques to avoid these problem. Like how can i set the appliocation to check if the server and client has thesame date before starting the application. Thanks
When inserting your records in the MySQL database you can use the NOW() function for the datetime field instead of passing in the current datetime from the client machine. NOW() will evaluate on the server and therefore be the server datetime.
https://www.w3schools.com/sql/func_mysql_now.asp
Have you thought about using an EPOCH timestamp?
Unix Time Stamp
Carrying this value instead of datetime values would provide a common base from which to determine the local (client) datetime values.
I am using DATETIME field to store my dates in MySQL. Without any timezone specified directly (there is just a datetime column)
But when timezone was changed on server MySQL updated all datetime columns according to new timezone.
Actually switching to EDT was the reason.
I don't need to recalculate my dates automatically - just want to store specific dates in it.
So even if tz changed manually to UTC from EST date should be same (from characters POV) if it was 2016-01-01 18:55 it should be same in any new tz..
I did not run any scripts\queries to update dates.
So it was performed either by MySQL itself or by server.
Need advice what I need to check to find and disable such feature.
Make sure you are using DATETIME and not TIMESTAMP
[From the MySQL documentation][1]:
MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)
You should also review this post (Duplicate):
Will changing a MySQL timezone change values of DateTime fields in a database?
I am sorry for the mess I brought
Just extended my query to grab some old dates - and it looks unchanged
So error defenetly in my code..
Anyway - thanks for your help
In MySQL a field was created with ON UPDATE CURRENT_TIMESTAMP designation. This saves the record update date in the server's timezone. I want to convert this time to UTC time.
The server is hosted in an account that doesn't have access to the server administration or the MySQL administration so I can't set the timezone on the server or MySQL directly through command line interfaces or startup files or any other way. This includes the SET time_zone command or the my.cnf file.
I also found a suggestion to use ON UPDATE UTC_TIMESTAMP but found this gives a syntax error and is not supported in the documentation.
So what I am trying to do now is to find a select clause that converts the value saved by ON UPDATE CURRENT_TIMESTAMP, which is saved in the server's current timezone, and convert it to UTC time.
Ideally this clause would detect the server's timezone so the value is not hardcoded and works with whatever timezone the server is set to.
Since nobody answered this question here is the workaround I ended up using:
Since I access mySQl from php I used php to solve the problem by calculating the difference in seconds from server time to UTC then subtracting that amount in the SQL statement.
Here is the PHP function to calculate the difference in seconds from server time to utc:
function utcOffset() {
$serverTime = new DateTime('NOW');
return $serverTime->getOffset();
}
Here is how I built the SQL statement in PHP to select time in utc in seconds since 1-1-1970:
$sql .= "SELECT TIMESTAMPDIFF(SECOND, '1970-01-01', myServerTimeStampField)-".utcOffset()." AS timeInUTCSeconds FROM myTable";
I went through lots of links but still I am confuse about the way that I could use.
I have mysql database at server side,when user hits server with some values, at same time I save current time of server also.(jsp is used at server side)
Now I want to update some values from row after specific interval of time from current time which saved in database.(Every row has different current time value.)
You will have to use the MySQL events. In this tutorial, you have an example of how to configure this via phpMyAdmin: http://www.youtube.com/watch?v=7ZRZoCsrKis.
My Java:
Date parsedDate = dateFormat.parse(timestamp);
Timestamp dbTimestamp = new Timestamp(parsedDate.getTime());
insertTimestampPreparedStatement.setTimestamp(3, dbTimestamp);
PhpMyAdmin/MySQL always displays this in my local time, even when I do
insertTimestampPreparedStatement.setTimestamp(3, dbTimestamp, Calendar.instance(TimeZone.getTimeZone("UTC"));
I'm not sure if MySQL stores them aware of timezone, and the only way I checked this was updating a timezone row to NOW() directly in phpmyadmin, and it then showed me a time in GMT. So either there is a bug in phpmyadmin/mysql or my code is not sending over timestamps in the correct timezone.
How do I get this to work in UTC?
A JDBC driver must use the local timezone when retrieving or storing timestamps, unless a Calendar with the specific timezone is provided.
Unfortunately, the JDBC spec isn't always clear on how drivers should behave, and it looks like MySQL does it wrong. For MySQL you can force the timezone it uses by specifying one or more options in the connection string serverTimeZone and related properties. See http://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html for a full list.