Convert datetime of a timezone to GMT in sql server - sql-server

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

Related

Get correct date from timestamp at a timezone

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

Convert UTC on Azure to EST Time (including Daylight saving Time-dynamic version)

I have a question about converting UTC time zone to EST time zone since I am still new to SQL language and Azure platform. In log file, I used system built-in function "GetDate()" in log file to get the Date/Time. However, while accessing Azure database on my SSMS, using system-built in function (GetDate()) gives me the datetime in UTC time zone, which is 4 hours ahead of Eastern Time Zone (EST). I have asked similar question before for converting UTC to EST here
, and #DanGuzman helped me fix my code. But this question is more about converting UTC to EST (considering Daylight saving time dynamically). Below is my code so far, and I used this link as a reference. However, I would like to make my code dynamic so that I can keep using it for 2020, 2021 as well.
Below code works ONLY for 2019 (since Daylight Saving starts on March 10,2019 until November 3, 2019. Within the date range, below code forward one hour of EST time during daylight saving time range.
CREATE FUNCTION [dbo].[EST_GetDateTime]
(
-- no parameter
)
RETURNS datetime
AS
BEGIN
DECLARE
#EST datetime,
#StandardOffset int,
#DST datetime, -- Daylight Saving Time
#SSM datetime, -- Second Sunday in March
#FSN datetime -- First Sunday in November
-- get DST Range
set #EST = CAST(DATEADD(hh,-5,GETDATE()) AS DATETIME)
set #StandardOffset = 0
set #SSM = datename(year,#EST) + '0310' -- Set which day daylight saving start (for 2019, March 10)
set #SSM = dateadd(hour,2,dateadd(day,datepart(dw,#SSM)*-1+1,#SSM))
set #FSN = datename(year,#EST) + '1103' -- Set which day daylight saving start (for 2019, March 10)
set #FSN = dateadd(second,-1,dateadd(hour,2,dateadd(day,datepart(dw,#FSN)*-1+1,#FSN)))
-- add an hour to #StandardOffset if #EST is in DST range
if #EST between #SSM and #FSN
set #StandardOffset = #StandardOffset + 1
-- convert to DST
set #EST = CAST(DATEADD(hh,-5+#StandardOffset ,GETDATE()) AS DATETIME)
RETURN #EST
END
GO
Can someone please give any suggestions how to improve my existing code (able to work dynamically) so that I do not need to change the function every single year for adjusting day light saving.
Since Azure SQL is ahead of the on-premises version, I think you can use the syntax select getutcdate() at time zone 'UTC' at time zone 'Eastern Standard Time'. Also, I'd switch to getutcdate() as that should be invariant regardless of the server's TZ. More information on at time zone here.
One method is a function that converts SYSDATETIMEOFFSET() to EST and converts the result to datetime:
CREATE FUNCTION dbo.EST_GetDateTime()
RETURNS datetime
AS
BEGIN
RETURN(SELECT CAST(SYSDATETIMEOFFSET() AT TIME ZONE 'Eastern Standard Time' AS datetime));
END
GO
--example usage
SELECT dbo.EST_GetDateTime();
GO
This should do exactly what you need.
SELECT CONVERT(DATETIME,GETDATE() AT TIME ZONE (SELECT CURRENT_TIMEZONE_ID()) AT TIME ZONE 'Eastern Standard Time')
Explanation: This gets the current datetime in the server's local timezone using CURRENT_TIMEZONE_ID(). We then use AT TIME ZONE to make it a datetimeoffset, then we cut that datetimeoffset over to requested timezone... here 'Eastern Standard Time'. Lastly the whole thing is wrapped in a CONVERT() to cut the datetimeoffset objects over to a proper datetime datatype.

Getting datetime from SQL Server into correct format for Redshift

When I extract a timestamp column from SQL Server, it comes across like this:
Thu Jan 26 2017 19:00:00 GMT-0500 (Eastern Standard Time)
I need to get it into an acceptable Redshift format. For example:
2017-01-26 19:00:00
How do I do this kind of conversion?
For a timestamp column X, use the FORMAT() function while converting to DATETIME to return a VARCHAR value:
SELECT FORMAT(CAST(X AS DATETIME), 'yyyy-MM-dd HH:MM:ss')
Or, you can also use CONVERT() to return a DATETIME value:
SELECT CONVERT(DATETIME, X, 120)

Sql Server Convert between UTC-0 string dates and the server local TimeZone

When I execute the following, when the server's TimeZone is +01:00:
Convert(datetime, '2015-02-10T23:00:00Z', 127)
The result is:
10.02.2015 23:00:00
That is the Date at UTC-0. My expected value would be 11.02.2015 00:00:00, that is the date converted to the server's TimeZone.
Convert function doesn't convert time to UTC. It just simply changes the format of the input string. Here is what you need to do.
Find difference in hours between server local time and UTC time:
DECLARE #hour INT
SELECT #hour = DATEDIFF(HOUR, GETUTCDATE(), GETDATE())
Add the difference to the date you're trying to convert:
SELECT CONVERT(DATETIME, DATEADD(hour, #hour, '2015-02-10T23:00:00Z'), 127)
If you know your timezone difference in hours and you know that it's unlikely to be changed, then use a shorter version:
SELECT CONVERT(DATETIME, DATEADD(hour, -1, '2015-02-10T23:00:00Z'), 127)
You shouldn't really depend on the time zone setting of a server. However, if you have a specific time zone in mind, you could use my SQL Server Time Zone Support project.
After installation:
SELECT Tzdb.UtcToLocal('2015-02-10T23:00:00Z', 'Europe/Paris')
Choose a time zone from the list here.

currenttimestamp with timezone in sql server

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.

Resources