Data transfer between SQL Servers in different timezones - sql-server

I transfer data from one SQL Server to another using SELECT * INTO .... statement (in Management Studio). Both databases have different timezone settings.
Is it possible to adjust all date/time fields to different timezone automatically? Maybe some query parameters or connection settings?

Since you are on SQL Server 2008, you could use the built-in functionality of the DATETIMEOFFSET datatype, and the SWITCHOFFSET function.
You could do something like:
INSERT INTO dbo.YourTargetTable( ...... )
SELECT
SWITCHOFFSET(TODATETIMEOFFSET(YourSourceTime, '+04:00'), '-09:00'),
.......
TODATETIMEOFFSET converts your "regular" date without any timezone information into a DATETIMEOFFSET type with a time zone (of your source location), and then you can apply a SWITCHOFFSET call to that value to "switch" your date/time to your target location's time zone.
This can be done in the scope of the INSERT....SELECT statement - no separate "row-by-row" updating necessary.

I think this will be helpful, please follow the link:
Effectively Converting dates between UTC and Local (ie. PST) time in SQL 2005
Shortly, you do need to build time zone and DST tables to get some help with time zones.
Thanks

Related

How to get timezone offset from the timezone name

I have a timezone name like America/Vancouver saved in a SQL Server database.
I want to get the UTC offset from the timezone name in SQL like America/Vancouver has -08:00 offset. So how can I write a query in SQL?
Your help is much appreciated
Unfortunately SQL Server doesn't yet support IANA time zone identifiers (like America/Vancouver) directly. For now, the best option is to convert from the IANA identifier to a corresponding Windows time zone identifier in your application layer, and store that in your SQL Server.
For example, if you are running .NET in your application layer you can use my TimeZoneConverter project:
string tz = TZConvert.IanaToWindows("America/Vancouver");
// Result: "Pacific Standard Time"
Then you can use the AT TIME ZONE function in your SQL Server code:
SELECT sysdatetimeoffset() AT TIME ZONE 'Pacific Standard Time'
The above will give you the current date, time, and offset in the given time zone, which you can use as the basis for your query against the user's availability (mentioned in the question comments).
Keep in mind that the offset adjustment will be different depending on whether daylight saving time is in effect or not. Despite the word "Standard" in the Windows identifier, DST is indeed taken into account where applicable.
Alternatively, if for some reason you must do this entirely in SQL Server without converting to Windows time zones, then you will need to rely on projects such as my SqlServerTimeZoneSupport project. That one is a bit old and not very well maintained, so I recommend against that approach if you can help it.
Use DATEPART with TZ parameter. Example :
SELECT DATEPART(tz, (CAST('2021-01-01' AS DATETIMEOFFSET) AT TIME ZONE 'Central European Standard Time'));
The result is in minutes.

MS SQL to Excel - Timezones

I have
an SQL Server 2012, where datetime fields are stored in UTC time
an Excel File, that is integrated via ADO DB command objects to the SQL server
a function to check the current system settings, which allows to see the current system's difference to UTC
I need to display dates/times in Excel in local time (varies).
Until SQL Server 2016 (which we can't currently upgrade to), there is no Timezone support built-in to T-SQL.
There are various options that are playing around in my head, but I can't decide which one is worth pursuing:
- transmit the current time difference as a parameter to the Stored Procedure, and amend all date fields to apply that difference
- retrieve the UTC times, and return the field(s) as a formula (so instead of return field, field, created, modified I might return field, field, =fnUtcToLocal(#created#)... and so on
- retrieve UTC times, and use an adjacent formula (with QueryTable.FillAdjacentFormulas)
Regardless of the option, I've still got a problem left: When, for example, checking the system difference now in Excel when the user is in Eastern Standard Time, it is -5 hours. When my created date falls into the EDT time, the correction would still be -5 instead of -4 hours.
Does anyone have a smart idea on how I should approach this? Please let me know if any additonal detail is required.

Convert stored utc dates to datetimeoffset datatype

We have thousands of historical UTC datetimes stored in a SQL Server database and we now want to change those columns to use the datetimeoffset datatype (currently datetime)
So I have altered the columns and changed the datatype. Now they all have a '+00:00' offset.
Since we are in NZ, I'm guessing next we would just update the data using SWITCHOFFSET with a '+12:00' offset?
However, that won't take into consideration DST, which makes the offset '+13:00' during DST periods?
Is there anyway to do this in SQL without a mapping table to DST switchover dates for the historical period?
Thanks
Keeping them as +00:00 is perfectly fine. However, if you want to project them to a particular time zone (including DST calculation), you can use the SwitchZone method from my SQL Server Time Zone Support project.
UPDATE yourtable SET dto = Tzdb.SwitchZone(dto, 'Pacific/Auckland')

SQL Server Timezone Change

I have 2 databases on the same SQL Server. Is it possible to have one in PST and the other in EST?
No, The date/time is derived from the operating system of the computer on which the instance of SQL Server is running.
You could however have a custom UDF that you would call instead of getdate() and then do the timezone change in that UDF. You can also assign default values to columns with something like this
CREATE TABLE Test (Val DATETIME DEFAULT dateadd(hh,-3,GETDATE()))
Now when you do an insert it will use the default
INSERT test DEFAULT VALUES
SELECT * FROM test
....this of course won't work on updates and also someone could update that value
If you want to use GMT then use GETUTCDATE
SELECT GETUTCDATE()
This does not account for daylight savings time. The only way this can be accomplished is by having a table for the time zones as this example.
https://github.com/mattjohnsonpint/SqlServerTimeZoneSupport
In newer SQL versions it's a lot easier but in older versions (2014 or older) there is no straightforward way to accomplish this.

Microsoft SQL Server 2005 time of day data type

Is there a data type which represents time of day? Such as a datetime data type without the date.
when i format 41 seconds into a datetime data type this is what I get:
1/1/1900 12:00:41 AM
I'm trying to store a duration of time in a table.
In SQL Server 2008, there is a Time data type which only stores the time. In versions of SQL Server prior to 2008, your only choice is the DateTime data type (and SmallDateTime) which stores both the date and the time. However, there are means in most languages including T-SQL to show the time portion. What are you trying to accomplish?
Not in 2005 no, there are plenty of discussions around on this subject.
This being just one of them

Resources