# Creating an expression Tag

**URL:** <https://forum.inductiveautomation.com/t/creating-an-expression-tag/10160>\
**Category:** Ignition\
**Created:** [June 23, 2015, 12:27pm UTC](https://forum.inductiveautomation.com/t/creating-an-expression-tag/10160 "2015-06-23T12:27:05Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mr.K001](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Mr.K001](https://forum.inductiveautomation.com/u/Mr.K001)\
**Post date:** [June 23, 2015, 12:27pm UTC](https://forum.inductiveautomation.com/t/creating-an-expression-tag/10160/1 "2015-06-23T12:27:05Z")

</div>

Hello, so i’m trying to output the right shift(A,B,C) based on the time of the day so i created an expression tag and changed it to string and in the expression field i put :

```auto
if dateExtract(now(0),"hour") >= 7 && dateExtract(now(0),"hour") < 15:
	print 'A'
elif dateExtract(now(0),"hour") >= 15 && dateExtract(now(0),"hour") < 23:
	print 'B'
else: 
	print 'C'
```

What am i doing wrong? doesn’t “dateExtract(now(0),“hour”)” extract the hour of the day?  
Whats weird is this mostly works fine when i try it in script playground and change the “dateExtract(now(0),“hour”)” to a variable x and give it a value. Thanks

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [June 23, 2015, 12:57pm UTC](https://forum.inductiveautomation.com/t/creating-an-expression-tag/10160/2 "2015-06-23T12:57:08Z")

</div>

It looks like you are on the right track using expression functions. “If” functions work differently in expression language than they do in Python/Jython.

To handle the three different possibilities you will have to use nested functions.

`
if(dateExtract(now(0),"hour") >= 7 && dateExtract(now(0),"hour") < 15,
	'A',
	if(dateExtract(now(0),"hour") >= 15 && dateExtract(now(0),"hour") < 23
	, 'B'
	, 'C'
	)
)`

The second if function goes in the “falseReturn” part of the outer if function.

[support.inductiveautomation.com … gic\_if.htm](https://support.inductiveautomation.com/usermanuals/ignition/gen_logic_if.htm)

---

<div class="post-metadata">

**Author:** ![Mr.K001](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Mr.K001](https://forum.inductiveautomation.com/u/Mr.K001)\
**Post date:** [June 23, 2015, 3:13pm UTC](https://forum.inductiveautomation.com/t/creating-an-expression-tag/10160/3 "2015-06-23T15:13:32Z")

</div>

wow, thank you. I was sitting here trying to figure it out and suddenly hits me that wait a min, don’t think python commands will work the same with expressions and that’s why it worked in the playground not in the tag expression. again, thanks for the reply! ![:thumb_right:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/2026e28c0c425034352c1b75807edf956adc75f4.gif ":thumb\_right:") ![:thumb_left:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/e26afb081ddde6df016df6ee49159cf56aaca688.gif ":thumb\_left:")

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [June 23, 2015, 3:18pm UTC](https://forum.inductiveautomation.com/t/creating-an-expression-tag/10160/4 "2015-06-23T15:18:40Z")

</div>

You’re welcome!

![:thumb_right:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/2026e28c0c425034352c1b75807edf956adc75f4.gif ":thumb\_right:") ![:afro:](https://us1.discourse-cdn.com/inductiveautomation/original/2X/3/3770a0bd4ab78c761044cebb66a2091a91530dd5.gif ":afro:") ![:thumb_left:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/e26afb081ddde6df016df6ee49159cf56aaca688.gif ":thumb\_left:")

---

<div class="post-metadata">

**Author:** ![Mr.K001](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Mr.K001](https://forum.inductiveautomation.com/u/Mr.K001)\
**Post date:** [June 23, 2015, 9:23pm UTC](https://forum.inductiveautomation.com/t/creating-an-expression-tag/10160/5 "2015-06-23T21:23:08Z")

</div>

another question, might as well give a brief explanation, i have 6 tags, one being the shifts that you helped me figure out earlier, i made a transaction group and put these 6 tags in it so i can make a customized table, everything is good, I’m trying to add some options here, one being adding a drop down calendar and have the user pick the date interval they want to see the data for. this is the query i try:

```auto
SELECT 
	Part_Number, 
	Running_Percentage, 
	Time_Percentage, 
	Cycle_Time, 
	Over_seconds, 
	t_stamp, 
        twelve_hour_Shifts 
FROM JK_LH_Monthly

WHERE 
        t_stamp BETWEEN {Root Container.Start.date} AND {Root Container.End.date};
```

and it doesn’t seem to work, i keep getting an error saying

```auto
Exception: Error running query:
SQLQuery(query=SELECT
	Part_Number, 
	Running_Percentage, 
	Time_Percentage, 
	Cycle_Time, 
	Over_seconds, 
	t_stamp, 
        twelve_hour_Shifts 
FROM JK_LH_Monthly

WHERE 
        t_stamp BETWEEN 2015-06-16 16:19:04 AND 2015-06-23 16:19:04;
, database =)@5000ms
on: JK_LH_Month.root container.Table.data
        caused by GatewayException: incorrect syntax near '16'.
        caused by SQLServerException: incorrect syntax near '16'.
```

What am I doing wrong? i thought that’s how you are suppose to use “BETWEEN” also, what does ‘16’ represent? line? character? Thanks in advance ![:mrgreen:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/d23659b203d2d24d394be983a52adb2fa6fe23d4.gif "Mr. Green")

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [June 23, 2015, 9:25pm UTC](https://forum.inductiveautomation.com/t/creating-an-expression-tag/10160/6 "2015-06-23T21:25:47Z")

</div>

Hi there,

Try adding single quotes around your dates, like this:

```auto
WHERE
        t_stamp BETWEEN '{Root Container.Start.date}' AND '{Root Container.End.date}';
```

16 represents the number 16 in your date.

---

<div class="post-metadata">

**Author:** ![Mr.K001](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Mr.K001](https://forum.inductiveautomation.com/u/Mr.K001)\
**Post date:** [June 23, 2015, 9:34pm UTC](https://forum.inductiveautomation.com/t/creating-an-expression-tag/10160/7 "2015-06-23T21:34:25Z")

</div>

umm, yeah of course it would be that easy 😮 … thanks a lot. its working now.

Now is it possible to make sure table opens up in client to the last row(most recent), when it opens now its always at the oldest row all the way at the top, i want it all the way at the bottom.

---

<div class="post-metadata">

**Author:** ![JGJohnson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jgjohnson/32/29582_2.png) [@JGJohnson](https://forum.inductiveautomation.com/u/JGJohnson)\
**Post date:** [June 23, 2015, 10:27pm UTC](https://forum.inductiveautomation.com/t/creating-an-expression-tag/10160/8 "2015-06-23T22:27:27Z")

</div>

This ought to do it. It would go in the table’s property change event

```auto
if event.propertyName == "data" and event.newValue is not None:
    table = event.source.parent.getComponent('Table')
    table.selectedRow = event.newValue.rowCount - 1
```

---

<div class="post-metadata">

**Author:** ![Mr.K001](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Mr.K001](https://forum.inductiveautomation.com/u/Mr.K001)\
**Post date:** [June 24, 2015, 1:24pm UTC](https://forum.inductiveautomation.com/t/creating-an-expression-tag/10160/9 "2015-06-24T13:24:07Z")

</div>

Thanks, that does work but i ended up changing it back since if for some reason I want to go up and look at a certain date it jumps right back the most recent date. but it worked Thanks.

I wrote this Query that to select data for my table from two popup calender

```auto
SELECT 
	t_stamp,
	Part_Number, 
	Running_Percentage, 
	Time_Percentage, 
	Cycle_Time, 
	seconds_for_current_part, 
	twelve_hour_Shifts 

FROM JK_LH_Monthly

WHERE
    t_stamp BETWEEN '{Root Container.Group 3.Start.date}' AND '{Root Container.Group 3.End.date}';

```

I would like to be able to pick a date and when picked have it show data from 7am to 6:59am next day(that’s how the shifts are 7am-7am), that way when i pick say June 23rd, i see everything from 7am on 23rd to 6:59am to 24th.  
I assume it works almost the same as above only difference would be in the WHERE clause but i cant use any functions. how about something like this:

```auto
WHERE 
     {Root Container.Day.date}
```

but maybe use the MAKETIME(07,00,00) function so its picks the day and make the time 7am

I’m still very new to SQL and learning as I go. ![:study:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/69117f8dc265a857fd146cb96caad7761d3a9157.gif ":study:") ![:scratch:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/76d1620bec252bfac07050c889ff5a91dc8a9c25.gif ":scratch:")

---

<div class="post-metadata">

**Author:** ![Mr.K001](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Mr.K001](https://forum.inductiveautomation.com/u/Mr.K001)\
**Post date:** [June 24, 2015, 1:51pm UTC](https://forum.inductiveautomation.com/t/creating-an-expression-tag/10160/10 "2015-06-24T13:51:45Z")

</div>

I know how to use dateArithmetic(date, - 7, “day”) which basically makes one date 7 days before the other but don’t know how i can do that and also tell it to make the hour, min, sec to a certain time i want.

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [August 3, 2015, 3:41pm UTC](https://forum.inductiveautomation.com/t/creating-an-expression-tag/10160/11 "2015-08-03T15:41:01Z")

</div>

You could add a custom property to each popup calendar and link it to an expression that reads the date and then adds on whatever time you want.

For example for the start date, add a string custom property called ‘datetime’ to the calendar and bind it to the following expression:`concat(dateFormat({Root Container.Group 3.Start.date}, "yyyy-MM-dd"), " 07:00:00")`Do the same for the end date with a time of “06:59:59”. You can then use these properties in your SQL expression i.e.`WHERE t_stamp BETWEEN '{Root Container.Group 3.Start.datetime}' AND '{Root Container.Group 3.End.datetime}';`
