# Compare t\_stamp with datatime tag in query

**URL:** <https://forum.inductiveautomation.com/t/compare-t-stamp-with-datatime-tag-in-query/62210>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [July 22, 2022, 12:41pm UTC](https://forum.inductiveautomation.com/t/compare-t-stamp-with-datatime-tag-in-query/62210 "2022-07-22T12:41:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [July 22, 2022, 12:41pm UTC](https://forum.inductiveautomation.com/t/compare-t-stamp-with-datatime-tag-in-query/62210/1 "2022-07-22T12:41:02Z")

</div>

Good morning guys,  
in perspective I have a DateTime Input components; its value props is binded to a DateTime memory tag (format string = “yyyy-MM-dd h:mm:ss aa” as default).

I create a query in a query tag and I’m trying to compare the date of my DateTime memory tag with the t\_stamp of the table, but it always return Error\_ExpressionEval.

The query works because if I test it with this comparison everything is ok:

```auto
WHERE DATE(t_stamp) = DATE(NOW())

```

I try to compare it in this way but it’s not working:

```auto
WHERE DATE(t_stamp) = DATE({[.]Giorno selezionato consumi fasce orarie.value})

```

---

<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:** [July 22, 2022, 1:35pm UTC](https://forum.inductiveautomation.com/t/compare-t-stamp-with-datatime-tag-in-query/62210/2 "2022-07-22T13:35:03Z")

</div>

Curly braces in queries perform string conversion on the given object before inserting into the query text. The object at that point is just a value, disconnected from its source, and therefore uses the default conversion for the object. For datetime values, this is almost never compatible with a database’s expectations. For anything but numbers, you also need single quotes in the SQL just outside the curly braces.

The correct answer is to use a query type that take parameters, either `?` parameters in “Prep”-style scripted queries, or `:named` parameters in named queries. These operations pass the value natively to the DB without string conversion. Note that an equals comparison is unlikely to work, as native Ignition date/time objects have millisecond precision.

If you absolutely must use curly braces, you will need an intermediate property to hold a deliberate string conversion of your datetime object, using the format your DB needs.

---

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [July 22, 2022, 3:36pm UTC](https://forum.inductiveautomation.com/t/compare-t-stamp-with-datatime-tag-in-query/62210/3 "2022-07-22T15:36:57Z")

</div>

Thanks for the explanation e for your time @pturmel .

Actually I must not use curly braces, that’s just how I thought to make it work, but if is not correct I can change without problem.

Use a named query is not a problem, I started to use them with perspective, but I can’t understand how to use a it in a query tag, I can’t find any menu to select it.

Eventually I could use a runScript in an expression tag?

Thanks again

---

<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:** [July 22, 2022, 4:08pm UTC](https://forum.inductiveautomation.com/t/compare-t-stamp-with-datatime-tag-in-query/62210/4 "2022-07-22T16:08:27Z")

</div>

For the moment, runScript is the only way to run a named query for a query expression tag.

---

<div class="post-metadata">

**Author:** ![craigb](https://avatars.discourse-cdn.com/v4/letter/c/9f8e36/32.png) [@craigb](https://forum.inductiveautomation.com/u/craigb)\
**Post date:** [July 22, 2022, 4:10pm UTC](https://forum.inductiveautomation.com/t/compare-t-stamp-with-datatime-tag-in-query/62210/5 "2022-07-22T16:10:33Z")

</div>

I tried this out and got it to work…

I formatted the date on the tag to be the below and that is bi-directonal binding with the date picker component.

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

Then my query is…

```auto
SELECT *
FROM dpr
WHERE DPRDate = '{[.]Date.value}'

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/7/177d4fadb1df7dbefeba74ae706e6fbd937d949b.png)

Not ideal as it goes against Phils advice on using curly braces but with my limited knowledge this is all I’ve got. DateTime comparison with databases has always been a trial and error for me.

As Phil suggest if you expand the date and use the Time also you results will just about always be zero as the database time stamp is down to the millisecond.

Im prepared to be shot down for the incorrect way of doing things 😀 But getting something working is a start…

---

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [July 22, 2022, 8:12pm UTC](https://forum.inductiveautomation.com/t/compare-t-stamp-with-datatime-tag-in-query/62210/6 "2022-07-22T20:12:41Z")

</div>

Thanks for the help, this works great!
