Get datetime for USA from local time - sql-server

I can able to get time of local sql server by GetDate() function. Sql server machine is in india.
Now I want to get the current datetime of USA on same sql server what can I do.

You need to use the SYSDATETIMEOFFSET call (instead of just GETDATE()) to get your local time with the timezone information:
SELECT SYSDATETIMEOFFSET()
and then you can use the SWITCHOFFSET function to define a new timezone you're interested in - based on UTC time. So the East Coast of the USA would be UTC -5 hours
SELECT SWITCHOFFSET(SYSDATETIMEOFFSET(), '-05:00') AS 'US East Coast'
while the West Coast is UTC -8 hours:
SELECT SWITCHOFFSET(SYSDATETIMEOFFSET(), '-08:00') AS 'US West Coast'

You can do it this way
dateadd(hh,differense_between_India_and_USA,getdate())
Time zones of the USA:
Pacific Standard Time (GMT -8)
Mountain Standard Time (GMT -7)
Central Standard Time (GMT -6)
Eastern Standard Time (GMT -5)
Alaska Standard time (GMT -9)
Hawaii Standard Time and Aleutian Standard Time (GMT -10)

You will get GMT from UTC time (Coordinated Universal Time)
SELECT SYSUTCDATETIME()

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

How to convert AT TIME ZONE return value to actual datetime value in SQL Server 2016?

I want to convert UTC value to Eastern Time Zone (any time zone) in SQL directly. I am trying below query but getting DATETIMEOFFSET.
I am trying to convert below UTC DateTime into Eastern time zone.
SELECT CONVERT(DATETIME,'2019-05-27 13:00:00' AT TIME ZONE 'Eastern Standard Time'
I am expecting output for above query is '2019-05-27 09:00:00' but the actual output is coming like '2019-05-27 13:00:00 -04:00'
you can do this
select CONVERT(datetime, SWITCHOFFSET(CONVERT(datetimeoffset, '2019-05-27 13:00:00'), DATEPART(TZOFFSET,
SYSDATETIMEOFFSET() AT TIME ZONE 'Eastern Standard Time')))
You're close. So far, you've told SQL Server the native time zone for the datetime that you've passed in. Now you need to tell it to convert it to something else. This should do the trick:
SELECT CONVERT(DATETIME,'2019-05-27 13:00:00')
AT TIME ZONE 'Eastern Standard Time'
AT TIME ZONE 'UTC';
This should give you something with the -00:00 offset. If you don't want that, cast the result back to a time zone-less datatype.

How to change the timezone in Azure SQL Database?

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

Convert datetime of a timezone to GMT in 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

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