# Microsoft SQL Server DATETIMEOFFSET

**URL:** <https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761>\
**Category:** Ignition\
**Created:** [November 7, 2019, 5:50pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761 "2019-11-07T17:50:11Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![joe.friedrich](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/joe.friedrich/32/7505_2.png) [@joe.friedrich](https://forum.inductiveautomation.com/u/joe.friedrich)\
**Post date:** [December 2, 2021, 6:23pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761/21 "2021-12-02T18:23:59Z")

</div>

Through trial and error, my colleague and I have found that the MSSQL JDBC driver included in 7.9 will return an ISO 8601 compliant string when reading a DATETIMEOFFSET column from MSSQL.

We just replaced the MSSQL JDBC driver in Ignition 8 with the (significantly older) MSSQL JDBC driver from Ignition 7.9.

Edit: Spelling

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [February 13, 2024, 7:21pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761/22 "2024-02-13T19:21:08Z")

</div>

When you say Ignition handles UTC to the daylight savings times beautifully, what do you do in your queries that makes it easy?

Something like this?

```auto
SELECT CONVERT(datetime, '2024-06-01 12:00:00') AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time' AS EasternTime

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 13, 2024, 7:32pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761/23 "2024-02-13T19:32:39Z")

</div>

> [@Zach\_Larson](#):
>
> do in your queries that makes it easy

Nothing. Doing anything but nothing makes it not easy. To do nothing and have Ignition time zones Just Work™, you need a database column type that either ...

- Holds UTC and converts strings to/from the connection timezone as needed, or
- Holds a timezone with the datetime value, and

... maps the column type to/from `java.util.Date` or `java.sql.Timestamp` in their JDBC driver.

In MS SQL Server, the `datetime2` column type meets this requirement. (It didn't use to, but does now.)

In PostgreSQL, the `timestamptz` aka `timestamp with time zone` column type meets this requirement.

In MariaDB, the `timestamp` column type meets this requirement.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [February 13, 2024, 7:37pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761/24 "2024-02-13T19:37:08Z")

</div>

I have a datetime column in UTC. Data collection was written to collect in UTC.

I need to use a where clause to filter for the shift start and end.  
What is the equivalent of doing nothing to make it work for me?

I am thinking that I have to do some kind of converting because UTC could be 4 or 5 hours too high depending on the time of year.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 13, 2024, 7:39pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761/25 "2024-02-13T19:39:54Z")

</div>

Change the datetime column to datetime2.

(I recommend changing Ignition's translators column types for "Datetime" to those in the prior comment.)

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [February 13, 2024, 8:02pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761/26 "2024-02-13T20:02:52Z")

</div>

I think you are saying the correct way is changing Ignitions translators datatype Datetime to a Datetime2.

I don't have permission yet to change that.  
If you do that, then UTC time would still be UTC time though correct?

I tried to cast the UTC time as a datetime2. I am sure the unseen part changed.  
I still need to do converting though to use a where clause on the UTC value though I think.

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/4/64cebd430739aa7e17c22b924eb95c495029fb51.png)

I am missing something that is not obvious to me.

* * *

Here is my where clause I want to use, but am stuck on currently.

```auto
Where
StartTimeUTC between 
case when DATEPART(HOUR, GETDATE()) < 7 then DATEADD(hh,-17,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0)) 
	 when DATEPART(HOUR, GETDATE()) >= 19 then DATEADD(hh, 7,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0))
										   else DATEADD(hh, -5,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0)) end
and

case when DATEPART(HOUR, GETDATE()) < 7 then DATEADD(hh, -5,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0)) 
	 when DATEPART(HOUR, GETDATE()) >= 19 then DATEADD(hh, 19,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0))
										   else DATEADD(hh, 7,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0)) end

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 13, 2024, 8:06pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761/27 "2024-02-13T20:06:41Z")

</div>

> [@Zach\_Larson](#):
>
> correct way is changing Ignitions translators datatype Datetime to a Datetime2.

That is a convenience that causes Ignition's table-creation helpers to use those column types. The critical change is to the table--change the column type.

Yes, it can be a big deal for a system in production. It is best to get the column types correct up front. (For a corporation that has facilities spanning multiple time zones, the plain MS SQL Server `datetime` column type is a terrible design choice. Your client's DB admins should be chastized.)

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [February 13, 2024, 8:24pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761/28 "2024-02-13T20:24:49Z")

</div>

I am probably going to get by using this conversion because I don't think I want to suggest changing the translator to datetime2 after years.

```auto
CAST(myDateTime AT TIME ZONE 'UTC' AT TIME ZONE 'Eastern Standard Time' AS DATETIME2) AS test

```

I tested it in a union of dates during the winter and dates in the summer.  
The conversions were correct for both with daylight savings.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [February 14, 2024, 3:06pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761/29 "2024-02-14T15:06:17Z")

</div>

Well this solution is not working.  
Taking too long when using cast.  
Trying to figure out a way to do it with convert which should be faster.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [February 14, 2024, 3:14pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761/30 "2024-02-14T15:14:58Z")

</div>

Cast the comparison values instead.  
Instead of this (pseudocode):

```auto
SELECT ...
FROM ...
WHERE :startDate >= CAST(t_stamp)

```

This forces SQL to CAST every record in the table and it can't use the column's index.

This code only has to do one cast and then can use the column's index:

```auto
SELECT ...
FROM ...
WHERE CAST(:startDate) >= t_stamp

```

I don't know how exactly you would write this.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [February 14, 2024, 3:40pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761/31 "2024-02-14T15:40:26Z")

</div>

Thanks Transistor

That is a great idea for some of my queries, or maybe all my subqueries.

* * *

Under 1/3rd second for 12 max timestamps so far.  
I wish it were faster though lol.

Ah I made a mistake. I think I saw pturmel about to post, and I spotted it.

```auto
 CONVERT(datetime, SWITCHOFFSET(
case when DATEPART(HOUR, GETDATE()) < 7 then DATEADD(hh,-17,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0)) 
			when DATEPART(HOUR, GETDATE()) >= 19 then DATEADD(hh, 7,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0))
												  else DATEADD(hh, -5,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0)) end   
                                              
, DATEPART(TZOFFSET,
	case when DATEPART(HOUR, GETDATE()) < 7 then DATEADD(hh,-17,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0)) 
			when DATEPART(HOUR, GETDATE()) >= 19 then DATEADD(hh, 7,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0))
												  else DATEADD(hh, -5,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0)) end 	
												  AT TIME ZONE 'UTC')))	

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 14, 2024, 3:56pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761/32 "2024-02-14T15:56:30Z")

</div>

> [@Zach\_Larson](#):
>
> half a second for 12 max timestamps so far

That's horrible. And there's a reason: You are converting your database column(s) to use with comparisons to various constants. Don't do that. Instead, convert your constants to be directly comparable to the _ **bare** _ column value.

If there are parts of your conditions that cannot be made to work that way, use a subquery to "prefilter" your source data using the comparisons that can be made efficient. Then the outer query can apply the more complicated WHERE clause(s) to just the rows that really matter.

With time-series data, it is especially important that comparisons to the `t_stamp` or equivalent column perform **no conversions** on that column.

(No, I didn't look close for mistakes, just commenting on the overall structure flaw.)

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [February 14, 2024, 4:06pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761/33 "2024-02-14T16:06:17Z")

</div>

Thanks  
150ms

I added in:

`between DATEADD(dd,-22,GETDate()) and GETDATE() and	`

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [February 14, 2024, 6:24pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761/34 "2024-02-14T18:24:29Z")

</div>

My convert statement above a couple posts did not work for converting to UTC.

When I use Getdate() I get the EST time.  
When I apply the conversion to UTC, the time stays the same.  
If I apply to convert to EST, then the date comes back wrong by 5 hours since it is winter right now in EST.

I need to rework my conversion technique on the constants that I derive from getdate().

* * *

I am prettty stuck as the db is deftinitely reading time system time in Eastern, but getdate() doesn't seem to have any tz. So ms sql keeps treating getdate() like it is UTC.

---

<div class="post-metadata">

**Author:** ![Zach\_Larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zach_larson/32/54388_2.png) [@Zach\_Larson](https://forum.inductiveautomation.com/u/Zach_Larson)\
**Post date:** [February 14, 2024, 8:14pm UTC](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761/35 "2024-02-14T20:14:02Z")

</div>

Adding the negative of the datedifference between getdate() and getDate() converted to EST won't work either.  
Because then a shift that overlaps dst will be short an hour.

I had to add some `at time zone 'Eastern Standard Time'` because getdate() doesn't have a timezone and getUTCdate() has a UTC date.

Finally got it working with

```auto
, CONVERT(datetime, SWITCHOFFSET(
case when DATEPART(HOUR, GETDATE()) < 7 then DATEADD(hh,-17,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0)) 
		when DATEPART(HOUR, GETDATE()) >= 19 then DATEADD(hh, 7,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0))
											  else DATEADD(hh, -5,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0)) end at time zone 'Eastern Standard Time' 
											  
, DATEPART(TZOFFSET,
case when DATEPART(HOUR, GETDATE()) < 7 then DATEADD(hh,-17,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0)) 
		when DATEPART(HOUR, GETDATE()) >= 19 then DATEADD(hh, 7,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0))
											  else DATEADD(hh, -5,DATEADD(dd,DATEDIFF(dd,0,GETDATE()-0),0)) end at time zone 'Eastern Standard Time' 
AT TIME ZONE 'UTC')))

```

[Previous page](https://forum.inductiveautomation.com/t/microsoft-sql-server-datetimeoffset/29761.md?page=1)
