# How do I ask SQL timestamp conditions?

**URL:** <https://forum.inductiveautomation.com/t/how-do-i-ask-sql-timestamp-conditions/52792>\
**Category:** Ignition\
**Created:** [November 5, 2021, 7:08pm UTC](https://forum.inductiveautomation.com/t/how-do-i-ask-sql-timestamp-conditions/52792 "2021-11-05T19:08:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [November 5, 2021, 7:08pm UTC](https://forum.inductiveautomation.com/t/how-do-i-ask-sql-timestamp-conditions/52792/1 "2021-11-05T19:08:04Z")

</div>

where t\_stamp between x and y

how do I properly ask SQL date questions related to t\_stamp?

Like if I wanted the count during the last 5 minutes  
or if I wanted the count during the month of october

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [November 5, 2021, 7:20pm UTC](https://forum.inductiveautomation.com/t/how-do-i-ask-sql-timestamp-conditions/52792/2 "2021-11-05T19:20:34Z")

</div>

> [@zacharyw.larson](#):
>
> Like if I wanted the count during the last 5 minutes

You didn't mention what flavour of SQL, so speaking pseudo;

```auto
datepart(now, -5, minutes)

```

or if I wanted the count during the month of october

```auto
DATEPART(mm, t_stamp) = 10

```

or your first line is perfectly valid, just longer;

```auto
where t_stamp between '2021-10-01' and 2021-10-31'

```

w3 schools etc is pretty good for this

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [November 5, 2021, 7:25pm UTC](https://forum.inductiveautomation.com/t/how-do-i-ask-sql-timestamp-conditions/52792/3 "2021-11-05T19:25:53Z")

</div>

Thanks, I am on the w3 page for date

however, I don’t see where it says like now

so I copied your example, switched the October to November, got an error of out of range

so how do you handle a now() scenario? Can you use now(30000)?

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [November 5, 2021, 7:27pm UTC](https://forum.inductiveautomation.com/t/how-do-i-ask-sql-timestamp-conditions/52792/4 "2021-11-05T19:27:37Z")

</div>

really odd, with 2021-11-31 I get an error

with 2021-12-01 I don’t get an error

edit: oh no  
oops

well all dates are going to be 1st to 1st now lol

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [November 5, 2021, 7:31pm UTC](https://forum.inductiveautomation.com/t/how-do-i-ask-sql-timestamp-conditions/52792/5 "2021-11-05T19:31:01Z")

</div>

MS SQL Server?  
MySql/Maria?  
PostGres?  
etc?

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [November 5, 2021, 7:31pm UTC](https://forum.inductiveautomation.com/t/how-do-i-ask-sql-timestamp-conditions/52792/6 "2021-11-05T19:31:28Z")

</div>

I think it is just regular sql, I am using sql bridge

---

<div class="post-metadata">

**Author:** ![Matrix\_Engineering](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/matrix_engineering/32/19781_2.png) [@Matrix\_Engineering](https://forum.inductiveautomation.com/u/Matrix_Engineering)\
**Post date:** [November 5, 2021, 7:32pm UTC](https://forum.inductiveautomation.com/t/how-do-i-ask-sql-timestamp-conditions/52792/7 "2021-11-05T19:32:32Z")

</div>

Try this and if it works it’s MS SQL

```auto
WHERE tstamp > DATEADD(minute, -5, GETUTCDATE())

```

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [November 5, 2021, 7:36pm UTC](https://forum.inductiveautomation.com/t/how-do-i-ask-sql-timestamp-conditions/52792/8 "2021-11-05T19:36:50Z")

</div>

```auto
(t_stamp between '2021-10-01' and '2021-11-01' or t_stamp > DATEADD(minute, -500, GETUTCDATE()));

```

both work, thanks very much

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [November 5, 2021, 7:37pm UTC](https://forum.inductiveautomation.com/t/how-do-i-ask-sql-timestamp-conditions/52792/9 "2021-11-05T19:37:54Z")

</div>

how often does the query get called if I bind it to a label on a perspective page though?

can I have it update every 30 seconds?

---

<div class="post-metadata">

**Author:** ![chandler](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@chandler](https://forum.inductiveautomation.com/u/chandler)\
**Post date:** [November 5, 2021, 7:41pm UTC](https://forum.inductiveautomation.com/t/how-do-i-ask-sql-timestamp-conditions/52792/10 "2021-11-05T19:41:54Z")

</div>

If you are using a query binding there is a polling option that you could set to 30 seconds.

If you are running the query from a script, then maybe you could create a custom property on your label, put the query binding on the custom property, then access the data from the custom property in your script.

---

<div class="post-metadata">

**Author:** ![zacharyw.larson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zacharyw.larson/32/23749_2.png) [@zacharyw.larson](https://forum.inductiveautomation.com/u/zacharyw.larson)\
**Post date:** [November 5, 2021, 7:44pm UTC](https://forum.inductiveautomation.com/t/how-do-i-ask-sql-timestamp-conditions/52792/11 "2021-11-05T19:44:29Z")

</div>

Thanks  
I will look for the setting in the binding I am using
