SQL Server AT TIME ZONE - sql

Using AT TIME ZONE is there a way to get my UTC Time without the 00:00 AT The end without using a LEFT in my query.
im doing this:
SELECT GETDATE() AT TIME ZONE 'EASTERN standard time' at time zone 'UTC'
Answer: 2018-03-05 15:08:00.930 +00:00
and trying to see if there is a better way other than doing
SELECT LEFT(GETDATE() AT TIME ZONE 'EASTERN standard time' at time zone 'UTC',23)

I would say declare it as variable and get datetime info from the datetimeoffset
Convert to datetime will do the trick
DECLARE #MyUtctime DATETIMEOFFSET
SET #MyUtctime = (
SELECT getdate() AT TIME ZONE 'EASTERN standard time' at TIME zone 'UTC'
)
SELECT CONVERT(DATETIME, #MyUtctime, 1)
SELECT getdate()

The following code will output your desired results:
CONVERT(VARCHAR, CONVERT(DATETIME, <DateField> AT TIME ZONE 'Eastern Standard Time' AT TIME ZONE 'UTC'), 100) AS [LocalDateTime]
If your EST date was '2018-03-22 22:48:24.893' the output UTC date would be 'Mar 22 2018 2:48AM'.

Related

How can I add/subtract the offset value into datetime and return new datetime value in SQL?

I am new to SQL and I want to add/subtract the offset value into the DateTime and return new date.
Currently, I am using this
SELECT
GETUTCDATE() AS UTCDate,
GETUTCDATE() AT TIME ZONE 'Eastern Standard Time'
which returns these values:
Current Result : 2021-12-28 07:19:39.320 -05:00
Expected Result : 2021-12-28 02:19:39.320
How can I achieve this? Any help would be appreciated. Thanks
You can use DATEADD and DATEDIFF function:
SELECT GETUTCDATE() AS UTCDate, GETUTCDATE() AT TIME ZONE 'Eastern
Standard Time' ,
DATEADD(MINUTE, DATEDIFF(MINUTE ,GETUTCDATE(), GETUTCDATE() AT TIME ZONE
'Eastern Standard Time')*-1, GETUTCDATE())
GETUTCDATE() returns datetime type. You can use SYSDATETIMEOFFSET() instead to get datetimeoffset and convert it to a desired timezone.
SELECT
SYSDATETIMEOFFSET() AS UTCDate_with_timezone,
SYSDATETIMEOFFSET() AT TIME ZONE 'Eastern Standard Time';
When AT TIME ZONE operator is applied to datetime value it does not change the value, you just inform the system what is the timezone. And this timezone will be just appended to the value.

Convert date column to new UTC date column

I wrote the following query on a SQL Server DB to convert a datetime column to a UTC date column.
select datetime
, dateadd(minute,-datepart(tz,datetime),datetime) datetime_dt_utc
from table1
But I get the same same datetime for both columns.
What do I have to change to make it work?
Please try this:
DATEADD(hh, DATEDIFF(hh, GETDATE(), GETUTCDATE()), datetime)
The GETDATE() will return the current time
The GETUTCDATE() will retunr the current UTC time
The DATEDIFF function will calculate the difference between this two datetimes in hours
The DATEADD function will add this hours to your current datetime.
Another way is via usign the AT TIME ZONE
SELECT datetime AT TIME ZONE 'UTC' from table1
You can check your time zone with:
select CURRENT_TIMEZONE ( )
And use this:
SELECT datetime AT TIME ZONE 'Pacific Standard Time' AT TIME ZONE 'UTC'
from table1
If you are in 'Pacific Standard Time' timezone

Calculate time at specific Timezone, from UTC Time

I want to calculate Datetime at given timezone based on Datetime in UTC.
I thought that I can do it with the following:
DECLARE #timeUTC DATETIME = '2019-01-01 10:00:00'
SELECT
#timeUTC AS timeUTC,
#timeUTC AT TIME ZONE 'Central European Standard Time' as at_time_zone_offset,
CONVERT(datetime, #timeUTC AT TIME ZONE 'Central European Standard Time',1) at_timezone_convert
-- OUTPUT
---timeUTC |at_time_zone_offset |at_timezone_convert
---2019-01-01 10:00:00.000 |2019-01-01 10:00:00.000 +01:00 |2019-01-01 09:00:00.000
The problem is that result of at_timezone_convert is incorrect- when at UTC time is 10:00, then time +1 is 11:00, not 9.
How can I get the result to be 2019-01-01 11:00:00.000?
At time zone documentation clearly states:
Converts an inputdate to the corresponding datetimeoffset value in the target time zone. When inputdate is provided without offset information, the function applies the offset of the time zone assuming that inputdate is in the target time zone. If inputdate is provided as a datetimeoffset value, then AT TIME ZONE clause converts it into the target time zone using the time zone conversion rules.
(emphasis mine)
If you'll declare #timeUTC as DateTimeOffset and not as DateTime you'll get different results - also, note that once you've converted the DateTimeOffset back to DateTime you'll get funky results.
Also, please note that the yyyy-mm-dd hh:mm:ss string representation format is a localized format when working with DateTime - that is not the case with the newer DateTime2 data type, which is one more reason why you should never work with DateTime again.
See a demo on DB<>Fiddle
Here's a trick I use from time to time:
DECLARE #timeUTC DATETIME = '2019-01-01 10:00:00'
SELECT #timeUTC
AS timeUTC, #timeUTC
AT TIME ZONE 'UTC'
AT TIME ZONE 'Central European Standard Time'
as at_time_zone_offset
Why does this work? Your original datetime has no offset information attached to it (other posters here have explained what the default is when this is the case) The first at time zone clause tells SQL Server "this datetime represents a time in UTC" and outputs a datetimeoffset data type. The second at time zone clause then tells it to convert it to your desired time zone.
Supplying the input as a datetimeoffset the AT TIME ZONE hint will convert to the input to the target time zone.
The snippet below is a simple example:
DECLARE #Utc DATETIME = '2019-01-01 10:00:00';
DECLARE #UtcOffset datetimeoffset(7) = #Utc;
SELECT
#Utc Utc,
#UtcOffset UtcOffset,
#UtcOffset AT TIME ZONE 'Central European Standard Time' UtcConverted;
-- Results
-- Utc 1/1/2019 10:00:00 AM
-- UtcOffset 1/1/2019 10:00:00 AM +00:00
-- UtcConverted 1/1/2019 11:00:00 AM +01:00
Zohar Peled explained it just fine, but just in case, here is a code example:
DECLARE #timeUTC DATETIME = '2019-01-01 10:00:00';
SELECT
#timeUTC AS timeUTC,
#timeUTC AT TIME ZONE 'Central European Standard Time' as at_time_zone_offset,
CONVERT(datetime, cast (#timeUTC AT TIME ZONE 'Central European Standard Time' as datetimeoffset),1) at_timezone_convert,
CAST(CAST(#timeUTC AS datetimeoffset) AT TIME ZONE 'Central European Standard Time' AS datetime) AS ResultYouNeeded;

SQL Server - Display EST as PST?

Server is in NY and date column (DateTime) is being filled as EST. How do users in California view this DateTime as the local PST?
I tried:
declare #dte DateTime = '20190919 10:01:01'
select #dte EST, #dte At Time Zone 'Pacific Standard Time' As PST
And I get
EST
2019-09-19 10:01:01.000
PST
2019-09-19 10:01:01.000 -07:00
I am looking for 2019-09-19 07:01:01.000 -07:00
Jeroen already said this in his comment, but I'm making the code concrete so you can see what he means.
Take your initial date, that you know the time zone for, but SQL Server doesn't yet, and tell it the time zone explicitly:
DECLARE #dte datetime = '20190919 10:01:01';
SELECT
#dte AT TIME ZONE 'Eastern Standard Time';
That returns a DATETIMEOFFSET value:
2019-09-19 10:01:01.000 -04:00
Next, convert that value to Pacific time by wrapping it in another AT TIME ZONE:
DECLARE #dte datetime = '20190919 10:01:01';
SELECT
(#dte AT TIME ZONE 'Eastern Standard Time') AT TIME ZONE 'Pacific Standard Time' AS datetime;
That returns another DATETIMEOFFSET:
2019-09-19 07:01:01.000 -07:00
Last but not least, for your presentation purposes, wrap that in a CAST so that you only get a DATETIME for your end users:
DECLARE #dte datetime = '20190919 10:01:01';
SELECT
CAST((#dte AT TIME ZONE 'Eastern Standard Time') AT TIME ZONE 'Pacific Standard Time' AS datetime);
Results:
2019-09-19 07:01:01.000
And Bob's your uncle.
Of course, if you want to take the lazy approach, DATEADD will get you where you want to be, too:
DECLARE #dte datetime = '20190919 10:01:01';
SELECT
DATEADD(HOUR, -3, #dte);
2019-09-19 07:01:01.000

Get HOUR from AT TIME ZONE

My DB is in Azure. I need my local (Israel) time.
SELECT GETDATE() AT TIME ZONE 'Israel Standard Time',
DATEPART(HOUR,(GETDATE() AT TIME ZONE 'Israel Standard Time'))
returns 13:
(No column name) (No column name)
2018-12-07 13:43:34.893 +02:00 13
I need it to return 15, since it's 13 (+2)=15
I want to do it without adding 2 hard-coded.
Try this:
SELECT GETDATE() AT TIME ZONE 'Israel Standard Time'
,DATEPART(HOUR,(GETDATE() AT TIME ZONE 'Israel Standard Time'))
,DATEPART(HOUR,(CONVERT(DATETIMEOFFSET, GETDATE(), 121) AT TIME ZONE 'Israel Standard Time'))