# Ms sql compare hours as integer

**URL:** <https://forum.inductiveautomation.com/t/ms-sql-compare-hours-as-integer/57544>\
**Category:** Ignition\
**Created:** [March 23, 2022, 3:59pm UTC](https://forum.inductiveautomation.com/t/ms-sql-compare-hours-as-integer/57544 "2022-03-23T15:59:33Z")\
**Posts on this page:** 5\
**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:** [March 23, 2022, 3:59pm UTC](https://forum.inductiveautomation.com/t/ms-sql-compare-hours-as-integer/57544/1 "2022-03-23T15:59:33Z")

</div>

My where clause is throwing errors when I try to see if the hour is after 20.

`where datepart(Hour,RecordedDate) > 20`

```auto
where cast(format(RecordedDate,'HH') as int) >20 

```

without the where clause the query works fine.

---

<div class="post-metadata">

**Author:** ![ethomason](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ethomason/32/43338_2.png) [@ethomason](https://forum.inductiveautomation.com/u/ethomason)\
**Post date:** [March 23, 2022, 4:06pm UTC](https://forum.inductiveautomation.com/t/ms-sql-compare-hours-as-integer/57544/2 "2022-03-23T16:06:27Z")

</div>

Works for me in MS SQL. Which SQL Server are you using? What’s the data type of RecordedDate and what error are you receiving?

---

<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:** [March 23, 2022, 4:09pm UTC](https://forum.inductiveautomation.com/t/ms-sql-compare-hours-as-integer/57544/3 "2022-03-23T16:09:23Z")

</div>

the sql bridge module , not sure of the year, ms sql

it is a datetime variable  
I can see the hours when I use the operators on it in the select section, or get the date if I try to

the error is

```auto
GatewayException: Incorrect syntax near the keyword 'where'.
	caused by SQLServerException: Incorrect syntax near the keyword 'where'.

```

---

<div class="post-metadata">

**Author:** ![ethomason](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/ethomason/32/43338_2.png) [@ethomason](https://forum.inductiveautomation.com/u/ethomason)\
**Post date:** [March 23, 2022, 4:13pm UTC](https://forum.inductiveautomation.com/t/ms-sql-compare-hours-as-integer/57544/4 "2022-03-23T16:13:21Z")

</div>

Can you post the full SQL statement? It looks like you have 2 WHERE statements.

---

<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:** [March 23, 2022, 4:13pm UTC](https://forum.inductiveautomation.com/t/ms-sql-compare-hours-as-integer/57544/5 "2022-03-23T16:13:56Z")

</div>

lol I just figured that out too

thanks so much, sorry
