Is there MySQL equivalent to Oracle's TIMESTAMP WITH TIME ZONE? - mysql

Is there MySQL equivalent to Oracle's TIMESTAMP WITH TIME ZONE?
I need to map a Oracle table, which has some columns with that datatype, into a MySQL table but I can't seem to find an easy way to do this without resorting to some MySQL functions.
Thanks and best regards.

No, you'll need to split the data into 2 columns, one a datetime, and the other holding the timezone information. But what you put in the latter field is dependant on what you've got stored in Oracle - the TIMESTAMP WITH TIME ZONE Datatype can contain the TZ offset and (optionally) the time zone region. Obviously the latter is a requirement for the date time to be semantically correct, but IIRC Oracle does not enforce this data being populated.
without resorting to some MySQL functions
Since MySQL doesn't have the datatype, it'll be very difficult to write MySQL function to process it - it's a lot simpler to create a MySQL compatible representation in Oracle where the datatype is supported. You just need to work out what data you've actually got and decide how you want to represent it in MySQL. By convention that means storing it in UTC along with the TZ in a seperate column, then convert it on selection with the convert_tz function (always from UTC)

MySQL always store timestamps as utc. Dates are always stored without timezone information.
You can configure mysql to return values from now() in different timezones.
To store the current offset you need to add this to some column on your own.

Related

How to export data from Cloud SQL to BigQuery on a daily basis?

I have created a connection to Cloud SQL and used EXTERNAL_QUERY() to export the data to Bigquery. My problem is that I do not know a computationally efficient way to export a new days data since the Cloud SQL table is not partitioned; however, it does have a date column date_field but it is of the datatype char.
I have tried running the following query with the view of scheduling a similar type so that it inserts the results: SELECT * FROM EXTERNAL_QUERY("connection", "SELECT period FROM table where date_field = cast(current_date() as char);") but it takes very long to run, whereas: SELECT * FROM EXTERNAL_QUERY("connection", "SELECT period FROM table where date_field = '2020-03-20';") is almost instant.
Firstly, it’s highly recommended to convert the ‘date_field’ column to the datatype DATE. This would improve simplicity and performance in the future.
When comparing two strings, MySQL will make use of indexes to speed up the queries. This is executed successfully when defining the string as ‘2020-03-20’ for example. When casting the current date to a string, it’s possible that the characters set used in the comparison aren’t the same, so indexes can’t be used.
You may want to check the characters set once current_datetime has been casted compared to the values in the ‘date_field’ column. You could then use this command instead of cast:
CONVERT(current_date() USING enter_char_sets_here)
Here is the documentation for the different casting functions.

Pure SQL solution to convert historical DateTime values to DateTimeOffset?

I have a large table with DateTime values that need to be converted to DateTimeOffset.
I can do this by using the following statement, which retains the date/time and adds the current time zone offset.
TODATETIMEOFFSET([StatisticDateUTC], DATENAME(tz, SYSDATETIMEOFFSET()))
The problem is some of these values represent dates years ago, some in daylight savings, some not, so it's actually incorrect to put the current offset in all of them. Some of them should have an offset of -700 some should have an offset of -800.
If the time zone was consistent for all of the values, how can I get the correct offset? I know how this can be done in .net, as there are a nice set of functions to do it, but I need a pure sql solution, no CLR functions.
If you can upgrade to SQL Server 2016, or use Azure SQL Database (v12), then you can use the new AT TIME ZONE function, which is very similar to the TimeZoneInfo.ConvertTime method you may be used to in .NET.
Otherwise, consider a third-party solution, such as my SQL Server Time Zone Support package, which uses standard IANA time zones.
More on both in this related answer.

what is the best way to format dates for MySQL

when inserting data into MySQL and dates are not in an agreeable SQL format like yyyy-mm-dd, what approach do you take to correct this column?
usually i write a custom ETL script to handle date formatting as data is being inserted, but i'm wondering if there a quicker and easier method? like something i could just do in mysql after the data is loaded?
for example, i have different files each with different date formats...
2013 Mar 05 23:26:32
01/03/2015
01/03/2015 23:26:32
annoying right?
when inserting data into MySQL and dates are not in an agreeable SQL format like yyyy-mm-dd, what approach do you take to correct this column?
Parse the dates on the client.
Pass the date as an object of type date.
Repeat for all the values I'm adding according to the required type in each case.
This has two benefits:
I don't need to know what format the database engine requires (which could be dependent on the locale the server happens to be using).
I don't need to worry about quoting of those values to avoid SQL injection attacks.
Summary: database clients understand how to send typed data to their database: make use of that functionality rather than re-creating it.

Convert MySQL times tamp column from one timezone to another

Need some ideas on how to convert an entire database TIMESTAMP columns from one timezone to another.
The current server is UTC, MySQL is also UTC so everything is good in that area. All time related columns are TIMESTAMPs. The problem is that when the time information was being entered, they were in EST/EDT. For example, enter start time: data is 1/1/2011 08:00:00 AM (EST/EDT). Because timezone wasn't implemented at the start, the database stored this as 08:00:00 UTC. So all the data in the database is stored like this. Once we get data that requires timezone info, this model will break.
The question is: how do you convert all these TIMESTAMP columns into the correct UTC time? The code will be changed to deal with this on the display side on a go-forward basis but what about historic data?
The simplest way seems to do a mysqldump --tz-utc of some sort and then import the data back, then release the code. However, I can't seem to find a good example of how to do this properly or if there are other ways to do this in the most efficient way possible.
Thanks!
Could you use the MySQL AddTime function to update the existing data?
UPDATE MyTable SET MyTimeColumn = ADDTIME (MyTimeColumn, -8:00:00) WHERE <the data is bad>

How to store and compare time-zone sensitive times

I have a data structure where an entity has times stored as an int (minutes into the day) for fast comparison. The entity also has a Foreign Key reference back to a TimeZone table which contains the .NET CLR ID Name and it's Standard Time/Daylight Time acronyms.
Since this information is stored as time-zone insensitive - I was wondering how in LINQ to SQL I could convert this into a UTC DateTime for comparison against other times that will be in UTC.
Just to be clear this conversion has to be done server-side so that I can execute filtering on the SQL Server and not the client. The reason for this is to ensure we take into account DST for time zones that support it.
I am using .NET 3.5 SP1 and SQL Server 2008.
Ideally, times should be stored in the database in UTC, and only converted to some local timezone (which would include a DST factor where appropriate) for display. This is especially true if "fast comparison" is your goal.
You might find it easiest to add an extra field which contains the UTC time, modify the clients to add this information, and run a script which one-time calculates it for existing entries.
I can store a CurrentOffset field that will need to be updated by a script that will be updated on the hour, every hour to determine the contextual offset.