We have data in SQL Server 2008, with multiple date time columns. As date format in SQL Server is a big integer value i.e. number of seconds since 1970, we are using DATEADD() function to convert it into actual date time format. But the issue we are facing is that when we use DATEADD(), we get data in UTC timezone whereas we want data in EST. I tried simply using -4 HR from the time but during NOV - MAR we are 1 HR ahead because the difference is 5 HR when daylight time is off. Is there any function that can be used which considers daylight saving time. Any help is appreciated.
Current Function -> DATEADD(s, columnName, '01/01/1970 00:00:00')
Regards, Sahil
try this:
DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), columnName)
You can use my SQL Server Time Zone Support project for this. It uses IANA standard time zone names, as listed here.
Building on your example, the following first converts from a UNIX Timestamp to a UTC based datetime, then converts to a datetime in the US Eastern time zone.
SELECT Tzdb.UtcToLocal(DATEADD(s, columnName, '1970-01-01 00:00:00'), 'America/New_York')
I would write a UDF that interacts with a time database such as the one from worldtimeserver.com -- I'm not affiliated with them and have found it with a quick internet search.
Further research shows timezonedb.com which has a free (if non-commercial) membership, allows to download the database and has examples on how to use with SQL Server and MySQL.
Related
I have a DATETIME column from the US/Pacific timezone, but it is not encoded as such. How can I convert this to UTC timezone in Azure SQL Data Warehouse?
The AT DATETIME T-SQL function seems like the closest fit, but it is not supported by Azure SQL Data Warehouse.
https://learn.microsoft.com/en-us/sql/t-sql/queries/at-time-zone-transact-sql
For example, one record has a DATETIME of 2013-04-02 08:02:47.000000. After conversion it should be 2013-04-02 15:02:47.000000.
Because my data were stored in 'US/Pacific' I used TODATETIMEOFFSET() to add the specific offset to the data. Once stored as a DATETIMEOFFSET type, it is treated as UTC time by the server but the timezone offset is still available.
SELECT TODATETIMEOFFSET(time_in_pt, '-08:00') as time_with_pt_timezone ...
https://learn.microsoft.com/en-us/sql/t-sql/functions/todatetimeoffset-transact-sql
it's a little hard to answer with no context, but i believe you could just cast or convert the column to whatever date/time type you desire. Accounting for the timezone is hard to say, yet again, with a lack of context.
Right now (in winter) we are in PST which is UTC – 8 hours. So converting your date 2013-04-02 08:02:47.000000 to UTC will be 2013-04-02 16:02:47.0000000 value.
declare #mydate datetime2 = '2013-04-02 08:02:47.000000'
select dateadd(hh, 8, #mydate) as utcdate
I have two questions regarding querying data in SQL that uses Epoch time stamps. I'm able to convert the date in my SELECT statement using this text:
DATEADD(HOUR, -4, DATEADD(SECOND, aa.fldTimeOfEvent, '1970-01-01 00:00:00'))
I had to use the "hour,-4" to convert it to Eastern daylight savings time. I'm curious if I will need to adjust this to -5 in November when daylight savings time ends? Is there a way to formulate the SELECT statement so it automatically adjusts for DST? This will be part of an automated report and I'm afraid we may miss changing this value.
Another question is how to use the WHERE statement to get data for a certain day. For instance, if I wanted the WHERE statement to grab all entries that occurred any time today (7/14/16), how would I do that? I've tried this statement:
WHERE DATEADD(HOUR, -4, DATEADD(SECOND, aa.fldTimeOfEvent, '1970-01-01 00:00:00')) = '2016-07-14'
It appears to only grab the entries that are exactly equal to midnight of that day (I think). I want all entries that occurred for any time the day.
Thanks in advance for your help.
The easy part first:
If you need to compare the date only, convert your datetimes to dates before comparing with: CONVERT(date, yourdatefield)
The second part has been discussed and solved here for UTC time: Convert Datetime column from UTC to local time in select statement. You would still need to add in the offset for Epochtime.
I'm assuming you're using SQL Server. If you only ever need the current offset, you can use GETUTCDATE() to calculate it:
SELECT DATEDIFF(hour,GetUTCDate(),GETDATE())
If you need to calculate the offset for other dates you'll likely want to use a CLR function: https://dba.stackexchange.com/questions/28187/how-can-i-get-the-correct-offset-between-utc-and-local-times-for-a-date-that-is
Which links to: https://blogs.msdn.microsoft.com/sqlserverfaq/2011/07/29/how-to-convert-utc-time-to-local-time-in-sql/
Using ASP.NET API I inserted DateTime using GetDate() SQL Server function, as I select back data it gives format like this 2015-12-07T09:20:17.097, and also the time is not correct to my current time while record inserted.
How can I get/insert my time according to local timezone
You have two options to do that:
Format data with SQL select query date formatter
Reference: http://www.w3schools.com/sql/func_convert.asp
With ASP.net c#/Vb.net, you may use Convert.ToDateTime(value).ToString("put format here");
Reference : https://msdn.microsoft.com/en-us/library/8kb3ddd4%28v=vs.110%29.aspx
There are two functions that can help you here. First is SYSDATETIMEOFFSET.
This returns the date, time and timezone from the server. If we combine this with SWITCHOFFSET we can return the date and time for another timezone.
Here's an example that returns US Eastern time (-05:00):
SELECT
SWITCHOFFSET(SYSDATETIMEOFFSET(), '-05:00') AS EasternTime
The time zone parameter is daylight saving awear.
This next example demonstrates that the time zone parameter sets the time zone, rather than just adding/subtracting the hours and minutes passed:
DECLARE #GMT DATETIMEOFFSET = '2015-12-17 09:00:00.0000000 +00:00'
DECLARE #GMT_Plus1 DATETIMEOFFSET = '2015-12-17 10:00:00.0000000 +01:00'
/* Both 9am in timezone +0 (London) and 10am in +1 (Paris)
* will return 4am Eastern time (timezone -5)
*/
SELECT
SWITCHOFFSET(#GMT, '-05:00') AS EasternTime1,
SWITCHOFFSET(#GMT_Plus1, '-05:00') AS EasternTime2
;
Both these functions require SQL Server 2008 or higher. For more on SQL Server's date and time functionality see MSDN.
My database have date values saved in GMT time zone in int format. I am trying to convert the date to local timezone, but the issue arises when the date is a past date, For instance in my case offset for date Dec 1, 2012 will be -5 and for June 15, 2010 will be -4 due to daylight savings. I am currently in EST.
So i need to know what was the UTC date of a previous date to be able to determine whether -4 or -5 will be the offset for that date.
SELECT Test_Number,
Last_Test_Date, dateAdd(hour,
datediff(hour, GETUTCDATE(), getdate()), --UTC offset
dateadd(second, Last_Test_Date, '1/1/1970 12:00 AM'))
FROM TestTable
I am not entirely sure if it is even possible. Any opinion ?
It sounds like you are actually looking to convert a UTC time stored in the database to a local date/time with the correct offset (-5 or -4).
There is no good way to do this in SQL Server. At least, not yet.
The best advice I can offer is to not do this in conversion in the database. Instead, pass the UTC value as-is back to your application code, and do the conversion there.
For example, if your application is written in .NET, you can use TimeZoneInfo or Noda Time to handle the conversions to any time zone you wish. If you're using something else, let me know and I will update the question.
How to get date and time difference between 10/12/2010 07:35:02 PM and 2010-11-19 21:51:01.713. Where first date is in MM-DD-YYYY format and Second date is in YYYY-MM-DD Format Rest is time it is also in different format as first format has "pm" in it. Please let me know how to write a query in sql 08 to calculate date and time difference?
The datetime data type in SQL Server is actually a 8-byte number. It may be represented in different formats to please humans but the format has no meaning to SQL Server itself.
To calculate the time difference between to datetime values you can use the built-in DATEDIFF function, which you can find details about here: http://technet.microsoft.com/en-us/library/ms189794.aspx
This will work thanks to the SQL Server ability to parse formatted dates for us:
select datediff(day, '10/12/2010 07:35:02 PM', '2010-11-19 21:51:01.713')
-----------
38