I have a (Azure) SQL Server database with timestamps in UTC. I want to get the date at a specific timezone.
The following shows the issue
DECLARE #TS DateTime2='2020-02-08 23:00:00'
SELECT CAST((#TS at time zone 'W. Europe Standard Time') as date) as StartDate, #TS as StartTimeStampUTC, #TS at time zone 'W. Europe Standard Time' as StartTimeStampLocalTime
StartTimeStampUTC : 2020-02-08 23:00:00.0000000
StartTimeStampLocalTime : 2020-02-08 23:00:00.0000000 +01:00
StartDate : 2020-02-08
I would have expected the StartDate value to be 2020-02-09, as local time is 2020-02-08 23:00 + 01:00 = 2020-02-09 00:00
How can I get the correct date?
You can follow the below-mentioned process in SQL Server.
1. Get UTC time using - GETUTCDATE()
2. Get difference between your DateTime and UTC DateTime using below link -
https://dzone.com/articles/dates-and-times-in-sql-server-at-time-zone
3. Add that difference in UTC DateTime
Please find below script -
SELECT GETDATE() AS 'Local Timezone',
GETUTCDATE() AS 'Utc_Timezone',
(DATEADD(HOUR,5,DATEADD(MINUTE,30,GETUTCDATE()))) as'Local Timezone based on UTC'
I have added 5:30 Hrs based on my local timeZone.
This seems to do the trick :
SELECT CAST(dateadd(hour,DATEdiff(hour,#TS AT TIME ZONE 'W. Europe Standard Time',#TS ),#TS) as date) as StartDate
answer is now 2020-02-09 as expected
Related
I have code in postgresql transform to sql server
In postgresql while inserting in table with data type timestamp with time zone in UTC format, it inserted with time zone
create table public.testt123 (tz timestamp with time zone)
insert into public.testt123
select now() at time zone 'utc'
select * from public.testt123
enter image description here
I have tried same with Sql server, below query
create table Test1(tz [datetimeoffset](7))
insert into Test1
select GETUTCDATE() AT TIME ZONE 'UTC'
enter image description here
It inserted without time zone, I have check using SYSDATETIMEOFFSET() but it gives time zone with current datetime not UTC
I have tried by left function, but it is correct way?
Select cast(left(SYSDATETIMEOFFSET() AT TIME ZONE 'UTC',28) + DATENAME(TZOFFSET, SYSDATETIMEOFFSET()) as [datetimeoffset](7))
enter image description here
Based on the comments, I suspect what you want is:
SELECT SYSUTCDATETIME() AT TIME ZONE 'UTC' AT TIME ZONE 'India Standard Time';
Though this could be abbreviated to:
SELECT SYSDATETIMEOFFSET() AT TIME ZONE 'India Standard Time';
I have check using SYSDATETIMEOFFSET() but it gives time zone with
current datetime not UTC
Correct, SYSDATETIMEOFFSET() returns a datetimeoffset but with the current UTC offset of the database server. Specify AT TIME ZONE 'UTC' to get a datetimeoffset with the UTC time with a zero offset:
SYSDATETIMEOFFSET() AT TIME ZONE 'UTC'
We would like to get the date in a specific location, either by using something like the C# solution:
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, "West US Time");
Or by setting the Azure SQL Database timezone and then using getdate().
From MSDN: Use AT TIME ZONE
SELECT CONVERT(datetime, '03/29/2015 01:01:00')
AT TIME ZONE 'Central European Standard Time';
--2015-03-29 01:01:00.000 +01:00
--Time between 02:00 and 03:00 is converted as +01!
SELECT CONVERT(datetime, '03/29/2015 02:01:00')
AT TIME ZONE 'Central European Standard Time';
--2015-03-29 02:01:00.000 +01:00
SELECT CONVERT(datetime, '03/29/2015 03:01:00')
AT TIME ZONE 'Central European Standard Time';
--2015-03-29 03:01:00.000 +02:00
What is the best way to get a local-time day name for a DATETIMEOFFSET field in SQL Server (2008+)? Is DATENAME(DW, DATEADD(mi, DATEPART(tz, #dt), #dt)) an acceptable approach to do this? It seems to work but is there a better way?
-- this is a Tuesday in London but still a Monday in PST
declare #dt datetimeoffset = '2016-4-12 01:14:00.00000 -08:00'
select TOP(1)
DATENAME(DW, #dt),
DATENAME(DW, DATEADD(mi, DATEPART(tz, #dt), #dt)),
DATENAME(DW, CAST(#dt AS DATETIME))
results in
Tuesday, Monday, Tuesday
The correct answer is Monday.
-- Corrected the value as -01:00
DECLARE #dt datetimeoffset = '2016-4-12 01:14:00.00000 -01:00'
SELECT
#dt AS OffSetForLondon,
DATENAME(DW, #dt) AS WeekNameLondon, -- London (-1:00)
DATENAME(DW, SWITCHOFFSET(#dt, '-08:00')) AS WeekNamePST -- PST (-8:00)
Actually, that value is Tuesday in both Pacific and London time zones, however neither are represented here. Allow me to explain.
When you read a datetimeoffset value, the date and time shown are the local time, as reflected by the offset specified. So, the value you provided is:
2016-04-12 01:14:00 -08:00
This is Tuesday, April 12th, 2016. It's 01:14 in Alaska time, because of daylight saving time being in effect. Pacific time is actually at -07:00 on this date, so the equivalent value there would be:
2016-04-12 02:14:00 -07:00
Again, still Tuesday, April 12th.
In UTC, that would be:
2016-04-12 09:14:00 +00:00
However, this is not the time in London, but rather the time in Iceland. London is +01:00 on this date, again due to daylight saving time. The equivalent time in London would be:
2016-04-12 10:14:00 +01:00
Again, still Tuesday.
To answer the actual question you asked, you can simply take the date part directly from the datetimeoffset value:
declare #dt datetimeoffset = '2016-04-12 01:14:00.00000 -08:00'
select DATENAME(DW, #dt)
Also, watch those leading zeros. ;)
I have a requirement of converting a record of time w.r.to timezone and compare it with current time.
For this, I want to convert datetime of a timezone to GMT timezone in SQL server.
i.e. (Jan 12 2015 11:30 A.M +5:30--->GMT standard time
Also is it possible to find out client's time zone from sql server?
If your datetime is stored as a datetimeoffset (or convertible to it) you can simply convert to a datetime2 (assuming you want to keep the precision of the datetimeoffset):
declare #dt datetimeoffset
select #dt = 'Jan 12 2015 11:30 AM +05:30'
select convert(datetime2, #dt, 1)
Which returns the time in UTC:
2015-01-12 06:00:00.0000000
This has nothing to do with the users' timezone, as it is simply doing the timezone to UTC calculation based on your provided offset.
SqlFiddle
What is the sql server query to retrieve current time stamp with us/central time zone?
You need both local and remote timezones, so Oracle can calculate the difference e.g.
SELECT FROM_TZ(CAST(sysdate AS TIMESTAMP), 'America/New_York')
AT TIME ZONE 'America/Los_Angeles'
FROM DUAL;
30.10.14 08:45:08,000000000 AMERICA/LOS_ANGELES
and
SELECT FROM_TZ(CAST(sysdate AS TIMESTAMP), 'America/New_York')
AT TIME ZONE 'America/Chicago'
FROM DUAL;
30.10.14 10:46:17,000000000 AMERICA/CHICAGO
You can get the list of timezone names with this:
SELECT tzname, tzabbrev FROM V$TIMEZONE_NAMES;
so you can do this:
SELECT FROM_TZ(CAST(sysdate AS TIMESTAMP), 'America/New_York')
AT TIME ZONE 'US/Central'
FROM DUAL;
assuming you are in New York.
If this is for SQL Server 2008 or above, this will provide the time at a specific timezone offset. I don't know if you can provide a timezone name, and using this method, you'll have to handle daylight savings yourself.
declare #dt datetimeoffset = switchoffset(convert(datetimeoffset, getutcdate()), '-06:00')
select getutcdate() as utc, #dt as [datetimeoffset], cast(#dt as datetime) as [datetime]
Time zone support was added with SQL Server 2016. You can now do this:
SELECT SYSDATETIMEOFFSET() AT TIME ZONE 'Central Standard Time'
Note that the identifier Central Standard Time is the Windows time zone ID for Central Time in US and Canada. It is inclusive of both CST and CDT.