# Scripting query help

**URL:** <https://forum.inductiveautomation.com/t/scripting-query-help/18189>\
**Category:** Ignition\
**Created:** [May 28, 2018, 4:37pm UTC](https://forum.inductiveautomation.com/t/scripting-query-help/18189 "2018-05-28T16:37:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![michaelfoxx852](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@michaelfoxx852](https://forum.inductiveautomation.com/u/michaelfoxx852)\
**Post date:** [May 28, 2018, 4:37pm UTC](https://forum.inductiveautomation.com/t/scripting-query-help/18189/1 "2018-05-28T16:37:46Z")

</div>

I am trying to build a query using scripting.  
The main problem is with the Select portion.

this works:

selectedQuarter = -1  
query = “SELECT DATEADD(QUARTER, DATEDIFF(QUARTER, 0, GETDATE()) -1, 0)”

rows = system.db.runQuery(query,‘historian’)

for row in rows:

```
startDate = row[0]
print startDate

```

query = “SELECT DATEADD(QUARTER, DATEDIFF(QUARTER, 0, GETDATE()), -1)”

rows = system.db.runQuery(query,‘historian’)

for row in rows:

```
endDate = row[0]

```

print endDate  
print selectedQuarter

this doesn’t:

selectedQuarter = int(event.source.parent.getComponent(‘Numeric Text Field’).intValue  
query = “SELECT DATEADD(QUARTER, DATEDIFF(QUARTER, 0, GETDATE()) [selectedQuarter], 0)”

rows = system.db.runQuery(query,‘historian’)

for row in rows:

```
startDate = row[0]
print startDate

```

query = “SELECT DATEADD(QUARTER, DATEDIFF(QUARTER, 0, GETDATE()), -1)”

rows = system.db.runQuery(query,‘historian’)

for row in rows:

```
endDate = row[0]

```

print endDate  
print selectedQuarter

Any thoughts?

---

<div class="post-metadata">

**Author:** ![Stuart](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@Stuart](https://forum.inductiveautomation.com/u/Stuart)\
**Post date:** [May 28, 2018, 5:49pm UTC](https://forum.inductiveautomation.com/t/scripting-query-help/18189/2 "2018-05-28T17:49:53Z")

</div>

> [@michaelfoxx852](#):
>
> selectedQuarter = int(event.source.parent.getComponent('Numeric Text Field').intValue

My first observation is that you're missing a closing parenthesis at the end of this line.

Also... I don't think that this

`query = "SELECT DATEADD(QUARTER, DATEDIFF(QUARTER, 0, GETDATE()) [selectedQuarter], 0)"`

is doing what you expect. It's possible I'm mistaken, but I suspect if you print that out `query`, you will get:

`SELECT DATEADD(QUARTER, DATEDIFF(QUARTER, 0, GETDATE()) [selectedQuarter], 0)`

The variable will not be inserted into the string. You need to use a different method to make it part of the string; check out [this link](https://stackoverflow.com/a/2962966/6483483).

---

<div class="post-metadata">

**Author:** ![michaelfoxx852](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@michaelfoxx852](https://forum.inductiveautomation.com/u/michaelfoxx852)\
**Post date:** [May 28, 2018, 6:33pm UTC](https://forum.inductiveautomation.com/t/scripting-query-help/18189/3 "2018-05-28T18:33:41Z")

</div>

Thanks Stuart, but it appears to be working, unless i typed something wrong in my original post.

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

---

<div class="post-metadata">

**Author:** ![Stuart](https://avatars.discourse-cdn.com/v4/letter/s/cab0a1/32.png) [@Stuart](https://forum.inductiveautomation.com/u/Stuart)\
**Post date:** [May 28, 2018, 6:37pm UTC](https://forum.inductiveautomation.com/t/scripting-query-help/18189/4 "2018-05-28T18:37:11Z")

</div>

Well… In that screenshot, you’ve commented out the line missing the parenthesis, and you’re never using the **`selectedQuarter`** variable!
