Azure SQL Convert "Time zone display name" to "Time zone ID" - azure-sql-database

I am working with the latest version of Azure SQL. I have a series of Time zone display name and want to convert those into Time zone ID. I know SQL server contains both the display name and the id. sys.time_zone_info has all the Time zone ID, but commands like CURRENT_TIMEZONE () output Time zone display name. What I am hoping for is a simple function that takes a string Time zone display name and returns the Time zone ID that name maps to, but I can't find any reference to that. And I can't find a table that actually stores the mapping.
Reference - Time zones in Azure SQL Managed Instance

In sys.time_zone_info, the name is the ID. Not the greatest for normalization purposes.
If you're using this to have your own internal time zone list for your application, then I think Joseph Xu has the correct answer.
If you're using this for queries against Azure SQL expecting time zone localization, bear in mind that, according to Azure SQL documentation, the database operates in UTC, and CURRENT_TIMEZONE_ID() will return UTC always.

I think we can create a table to store the infos with two columns Time zone ID and Time zone display name. Then create a stored procedure to query that, input Time zone display name return Time zone ID.

As I know, SQL Azure does not provide tables to convert from Time Zone ID to Time Zone display name. I think there is not any table with that information because the sys.time_zone_info system view obtains the information from internal functions instead of other tables you can check.
You may use some tables and functions from the T-SQL Toolbox or you may create your own tables.
Using a custom table
You may obtain (and/or modify) the code to create the tables from the TSqlToolbox. This code creates a table DateTimeUtil.Timezone with the desired information.
I made an example you may try in db<>fiddle
SELECT identifier
FROM [TimeZone]
WHERE [TimeZone].[DisplayName] = '(UTC-08: 00) Baja California'
| Identifier |
|----------------------------------|
| Pacific Standard Time (Mexico) |

Related

Is there any way to get only "-04" or the difference value based on whatever time zone is added in a query?

Is there any way to get only "-04" or the difference value based on whatever time zone is added in a query?
select convert(datetime,GETDATE()) at time zone 'Eastern Standard Time'
Result: 2017-09-12 17:49:18.377 -04:00
if you are using sql server you can do this one of two ways depending on the data you want
datepart(tz,SYSDATETIMEOFFSET()) or datename(tz,SYSDATETIMEOFFSET()).
if you take a look at these functions you will notice they require offsets. GETDATE() does not return the correct type.
you will have to use SYSDATETIMEOFFSET() instead.
for more information on this please read this

How to change the default timezone in Amazon Redshift?

Setting a timestamp column to SYSDATE by default, stores it as UTC. Is it possible to change the timezone so SYSDATE stores dates and times to a different timezone?
So far, I've checked the SET command but I'm not sure if it is possible to use it to change the timezone.
Basically, the answer is no. According to the documentation:
TIMESTAMP values are UTC, not local time, in both user tables and
Amazon Redshift system tables.
Note Timestamps with time zones are not supported.
If you need the queries to return data in different timezone, you can use the CONVERT_TIMEZONE function, with either constant timezone value (or query parameter), or joining the result to a configuration table which contains the required timezone.

SQL - Inserting a time value into column?

I am using Oracle SQL and I would like to insert a time value (eg 15:45 or 15:45:00) into a column which has a data type of TIMESTAMP. I have tried the following but It gives a error about it not being a valid month.
INSERT trainTbl(Dest, trainTime)
VALUES
('Waterloo', '15:00:00');
Would appreciate if someone could put me on the right direction.
Thanks
Oracle's TIMESTAMP data type holds a complete time and date. You cannot use it to store a time only; either store a complete time and date, or use a different data type for your column.
Some options for ways to store the time only are discussed in How to store only time; not date and time?. However, if you're already storing the date in another column, you should probably just store this information together. There is a reason Oracle provides the data types that it does.

SQLite and inserting the current date in UTC format

How do I use an SQL statement on an sqllite database to insert the current date in UTC. I found the NOW function but what format is that in? This will be on mobile devices so everyone will have a different locale, however, I need a standard time format because the device will compare the dates with my server.
Also, is there a way to automatically update a 'modified' field when the data in the row is changed like you can in MySQL?
SELECT DATETIME('now') returns the current UTC datetime. See Date And Time Functions. You can use DATETIME DEFAULT CURRENT_TIMESTAMP with column declaration.
Format 11, the string 'now', is
converted into the current date and
time as obtained from the xCurrentTime
method of the sqlite3_vfs object in
use. Universal Coordinated Time (UTC)
is used
For the 'modified' field you can use a trigger.
You don't specify what you use to develop your application on. I prefer using QDate::toJulianDay and QDate::fromJulianDay in Qt to store dates in an SQLite database as an integer if I only need to store the date.

How to convert a SqlServer DateTime to universal time using SQL

I have a database with DateTime fields that are currently stored in local time. An upcoming project will require all these dates to be converted to universal time. Rather than writing a c# app to convert these times to universal time, I'd rather use available sqlserver/sql features to accurately convert these dates to universal time so I only need an update script. To be accurate, the conversion would need to account for Daylight savings time fluctuations, etc.
A User Defined Function would allow you to write an SQL query that looks like this:
SELECT toUTC([MyDateColumn], [MyTimeZoneColumn]) FROM [MyTable]
Then you get universal times back from the server without a lot of ugly syntax in the query itself. Now you could build the UDF for this with regular SQL similar to what Chris posted, but SQL Server 2005 and later will let you build the UDF using CLR (.Net: C# optional) instead. It has much better support for dates and can do a better job taking timezones and daylight savings time into account.
check out the convert function and the getutcdate function?
http://msdn.microsoft.com/en-us/library/ms187928.aspx
Check out this link from CodeProject.com - it does exactly what you want: take a date and a time zone, pass them to a UDF, and get the date in UTC or any other time zone.
IMPORTANT: Check the comments of that article - the author wasn't allowed to revise the article after a certain point, and there is an updated version of the code used for the UDFs in the comments that addresses some issues not found in the original article code.
ALSO IMPORTANT: Don't use this for querying large data sets. It's perfectly fine for a one-time load into a database, or for returning a UTC date for a single row (like a user login table or what have you.)
If you want performance, the only really acceptable method for time zone conversion is to have a lookup table that handles every possible time zone conversion for every single hour in a year, with a case statement to handle rollovers between years (ie December 31 - January 1 or vice versa.) Yes, the table is huge, but the query performance is nil.
SQL Doesn't have anything built in for this.
Two ways would be the C# application (you mentioned you don't want) or writing a really complicated update statement with something like:
UtcDate = DATEADD(hour, CASE WHEN OriginalDate BETWEEN x AND y THEN 4
WHEN OriginalDate BETWEEN x2 AND y2 THEN 5 ... END, OriginalDate)
Note - I'd recommend the C# app plus something like TZ4Net to handle the conversion.