# Data Range SQL Query

**URL:** <https://forum.inductiveautomation.com/t/data-range-sql-query/18140>\
**Category:** Ignition\
**Created:** [May 23, 2018, 4:23pm UTC](https://forum.inductiveautomation.com/t/data-range-sql-query/18140 "2018-05-23T16:23:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndutra](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@ndutra](https://forum.inductiveautomation.com/u/ndutra)\
**Post date:** [May 23, 2018, 4:23pm UTC](https://forum.inductiveautomation.com/t/data-range-sql-query/18140/1 "2018-05-23T16:23:56Z")

</div>

Using an example code snippet from the Ignition Manual I am using a DateRange component to filter table data based on time in an SQL query, as soon as I close the script window I get an error with the query. The Details are Incorrect Syntax, can anyone help out? I’m not sure what I’m doing wrong, FYI the column that is being filtered is based off the tag var LastChange. See Below…

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/c/c8fa54beacd3ddad3de6c33e6ac7e2286b452be8.jpg)

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/4/4b7a00d33e7cf3993718260340f19f785ffa87c2.jpg)

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [May 23, 2018, 4:28pm UTC](https://forum.inductiveautomation.com/t/data-range-sql-query/18140/2 "2018-05-23T16:28:11Z")

</div>

Put single quotes around the control values.

> '{Root Container.Date Range.startDate}'

---

<div class="post-metadata">

**Author:** ![ndutra](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@ndutra](https://forum.inductiveautomation.com/u/ndutra)\
**Post date:** [May 23, 2018, 4:35pm UTC](https://forum.inductiveautomation.com/t/data-range-sql-query/18140/3 "2018-05-23T16:35:45Z")

</div>

That seemed to work, I get no errors. But my data is gone now? The time string format is the same.

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/0/06f7dcd75c2330cf170dd912c1e129089189a2e2.jpg)  
 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/9/9c993fdfd68ee73e750d6883850b6859d8e34a06.jpg)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 23, 2018, 4:40pm UTC](https://forum.inductiveautomation.com/t/data-range-sql-query/18140/4 "2018-05-23T16:40:46Z")

</div>

There’s probably a difference in format syntax that is causing something unexpected. Try checking what the output looks like with something like:

```auto
SELECT '{Root Container.Date Range.startDate}'

```

Then insert the date formatted string into our original query to see whether it’s a date format the RDBMS expects (usually, DBs will only automatically cast strings -\> dates in very specific formats)

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [May 23, 2018, 4:43pm UTC](https://forum.inductiveautomation.com/t/data-range-sql-query/18140/5 "2018-05-23T16:43:41Z")

</div>

Also I see now that you are selecting on a timestamp field.  
Is that an actual SQL Timestamp field or a datetime field you named that?

Actual timestamp fields aren’t designed to be used as datetime fields.

---

<div class="post-metadata">

**Author:** ![ndutra](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@ndutra](https://forum.inductiveautomation.com/u/ndutra)\
**Post date:** [May 23, 2018, 4:48pm UTC](https://forum.inductiveautomation.com/t/data-range-sql-query/18140/6 "2018-05-23T16:48:02Z")

</div>

I am selected a column which is being populated by the tag value of LastChanged, see below… both I believe are string values in SQL: varchar(255)

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/d/df3d314b0529e872292ef912926bedb2d293b2d6.jpg)

---

<div class="post-metadata">

**Author:** ![ndutra](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@ndutra](https://forum.inductiveautomation.com/u/ndutra)\
**Post date:** [May 23, 2018, 4:59pm UTC](https://forum.inductiveautomation.com/t/data-range-sql-query/18140/7 "2018-05-23T16:59:01Z")

</div>

I do have a formatting issue it will not allow me to insert the daterange value into the DB column, do you mean if I formatted the LastChange tag to match the DateRange or vise-versa this should work?

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/f/f7700c5075ea1b4e1e2654031ea0102ffbcbca2c.jpg)

---

<div class="post-metadata">

**Author:** ![ndutra](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@ndutra](https://forum.inductiveautomation.com/u/ndutra)\
**Post date:** [May 23, 2018, 5:16pm UTC](https://forum.inductiveautomation.com/t/data-range-sql-query/18140/8 "2018-05-23T17:16:19Z")

</div>

Worked! Thank you

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/c/cf179e8d718a95d504a4a4ef082e2205097bf2e0.jpg)

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [May 23, 2018, 5:16pm UTC](https://forum.inductiveautomation.com/t/data-range-sql-query/18140/9 "2018-05-23T17:16:48Z")

</div>

Since you have a space in the table name you will have to wrap that in quotes on your insert or brackets.

> INSERT INTO [Backflow Preventer]  
> or  
> INSERT INTO 'Backflow Preventer'

---

<div class="post-metadata">

**Author:** ![ndutra](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@ndutra](https://forum.inductiveautomation.com/u/ndutra)\
**Post date:** [May 23, 2018, 5:18pm UTC](https://forum.inductiveautomation.com/t/data-range-sql-query/18140/10 "2018-05-23T17:18:32Z")

</div>

I mistyped the table name, it should be Backflow\_Calibration, it ended up being the format of times were mismatched I added the system.date.format to make them line up. Thank you for your time!
