Get date at a provided timezone with daylight savings using MSSQL query - sql-server

I am using SQL Server 2014
Currently, I am using MSSQL function:
format(TODATETIMEOFFSET(("[dateColumn]",'-6:00'),'MM/dd/yyyy')
However, this will not apply Daylight savings.
How can i achieve it?
Please help

This answer was provided before information was received that the OP was using SQL Server 2014 (which I have now added to their tags). I have left the answer here, however, as it will likely be helpful for future readers using a more recent version of SQL Server.
UTC -6 will always be UTC -6; it doesn't change for daylight savings and become UTC -5. Timezones like GMT (UTC+0) and EST (UTC-5) change to BST and EDT but then they also respectively become UTC +1 AND UTC -4. You're providing a literal number so the value will always be UTC -6.
What you are likely looking for is AT TIME ZONE (AT TIME ZONE (Transact-SQL), which was introduced with SQL Server 2016. For example:
--Returns 2018-01-01 18:00:00 -06:00
SELECT CONVERT(datetime2(0), '2018-01-01T18:00:00') AT TIME ZONE 'Central Standard Time';
--Returns 2018-07-01 18:00:00 -05:00 (changes to -5, due to daylight saving)
SELECT CONVERT(datetime2(0), '2018-07-01T18:00:00') AT TIME ZONE 'Central Standard Time';

Related

SQL Server - convert UTC to EST (include Daylight Savings, when appropriate)

I have a table that stores the transaction date time in UTC time zone. I would need to convert this time to eastern time zone i.e. EST or EDT, depending on the transaction date.
How can I do this without writing a big function or creating a table / view that flags EST / EDT for each transaction date?
Use AT TIME ZONE, which changes the timezone of a value to the relevant time there, and observes Daylight Saving:
SELECT CONVERT(datetimeoffset(0),'2021-09-07T15:25:37+00:00') AT TIME ZONE 'Eastern Standard Time' AS Sep21,
CONVERT(datetimeoffset(0),'2021-01-09T15:25:37+00:00') AT TIME ZONE 'Eastern Standard Time' AS Jan21;
Which returns the following:
Sep21 Jan21
---------------------------------- ----------------------------------
2021-09-07 11:25:37 -04:00 2021-01-09 10:25:37 -05:00

Convert UTC Time to Multiple Time zones

The below SQL will convert the UTC time to my local time in BRISBANE, Australia.
I would like to get the Local time in Sydney which is 1 hour ahead of Brisbane time considering the DST
SELECT
GETUTCDATE() AS UTCTime,
CAST(GETUTCDATE() AT TIME ZONE 'UTC' AT TIME ZONE 'E. Australia Standard Time' AS DATETIME2(2)) AS BrisbaneTime
Need some assistance in getting the local time in Sydney.
All of the possible timezones are defined in the sys.time_zone_info system table. From there you can select the appropriate timeone to use.
You would use the same query but with the other timezone

Save Date on SQL Server with wrong timezone

I have this:
Clock = Date.now();
this.newReport.DateTime = new Date(this.Clock);
Client side the date is ok: Mon May 20 2019 19:08:34 GMT+0200
But on SQL Server it is save with time 17:08
Why?
Thanks
It looks like that's the same date, when you include the Timezone offset. If the "client side" date is GMT +0200, then the datetime stored should be 17:08:34 (as GMT time).
You're not including much detail, but I expect that you are wanting to save the time as local time. You could either convert the time to Localtime before saving it (and lose the additional information about the timezone), or save the GMT offset along with the date and time so that you could have both available if you wanted to convert it back to localtime later.

Azure SQL display local time

I'm trying to display datetime values in local time. By default, Azure SQL Database stores dates and times in UTC, there is no way around this. (This was a pain when migrating from on premise SQL Server.) I would like to display a stored time value in Central European time.
Now the local time is 11:30 (CET). UTC time is 10:30.
DECLARE #TestTime DATETIME;
SET #TestTime = '2016-11-02 10:30:00'
SELECT #TestTime
--Returns 2016-11-02 10:30:00
SELECT #TestTime AT TIME ZONE 'Central European Standard Time'
--Returns 02 November 2016 10:30:00 +01:00
I need to return 2016-11-02 11:30:00 somehow. Now for the fun part:
As has been suggested here:
SELECT convert(DATETIME,#TestTime AT TIME ZONE 'Central European Standard Time',1)
--Returns 2016-11-02 09:30:00 So instead of adding the timezonedifference it subtracts it.
This works, but makes me sick:
SELECT DATEADD(MINUTE,DATEPART(tz,#TestTime AT TIME ZONE 'Central European Standard Time'),#TestTime)
--Returns 2016-11-02 11:30:00
A similar solution has been suggested here, but it plays with string operations. My suspicion is that something is wrong in AT TIME ZONE; it should have displayed 11:30 +1, and not 10:30 +1, no?
Is there really no proper way to display a UTC time in local time? This "hacking around it" feels awfully dirty, especially since at any point in time it just might stop working (e.g. Microsoft fixes / introduces a bug).
Thanks!
The AT TIME ZONE statement performs two distinct operations:
To assert that a datetime (or datetime2) is in a particular time zone, thus looking up the correct offset for that zone and returning a datetimeoffset value with the correct offset applied.
To convert a datetimeoffset value to a different time zone, using the offset from the source value to pin down an exact point in time, then looking up the new offset for the requested time zone and applying it. This returns a datetimeoffset with a potentially different local time and offset than the original, but representing the same moment in time.
You are using the first part only. To convert a datetime from UTC to a specific time zone, you'll need to use both.
SELECT #TestTime AT TIME ZONE 'UTC' AT TIME ZONE 'Central European Standard Time'
The first AT TIME ZONE asserts that the input datetime is in UTC, resulting in a datetimeoffset that has +00:00 for the offset. The second AT TIME ZONE converts that to the time zone requested.

Convert like America/New_York in MySQL server 2008 R2 server

13:30:00 Friday April 19, 2013 in America/New_York converts to
09:30:00 Friday April 19, 2013 in America/Anchorage
Convert like this in SQL Server 2008 R2.
using select query.
Please help me on this.
Thanks,
we will convert in java wright from "America/New_York" to America/Anchorage like way i need a select query to convert EST time to other time zones..please not the time zone cannot be given like MET,GMT and all...
PLEASE NOTE:- All time zones will only be like plain text America/Anchorage
You are looking for this function:
CONVERT_TZ(dt,from_tz,to_tz)
CONVERT_TZ() converts a datetime value dt from the time zone given by from_tz to the time zone given by to_tz and returns the resulting value. Time zones are specified as described in Section 10.6, “MySQL Server Time Zone Support”. This function returns NULL if the arguments are invalid.
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_convert-tz

Resources