# MS SQL Queries: Getting Current Date and Using Date Arithmetic to Filter Database Results

**URL:** <https://forum.inductiveautomation.com/t/ms-sql-queries-getting-current-date-and-using-date-arithmetic-to-filter-database-results/23755>\
**Category:** Ignition\
**Created:** [April 1, 2019, 5:26pm UTC](https://forum.inductiveautomation.com/t/ms-sql-queries-getting-current-date-and-using-date-arithmetic-to-filter-database-results/23755 "2019-04-01T17:26:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![EEdmondson](https://avatars.discourse-cdn.com/v4/letter/e/41988e/32.png) [@EEdmondson](https://forum.inductiveautomation.com/u/EEdmondson)\
**Post date:** [April 1, 2019, 5:26pm UTC](https://forum.inductiveautomation.com/t/ms-sql-queries-getting-current-date-and-using-date-arithmetic-to-filter-database-results/23755/1 "2019-04-01T17:26:58Z")

</div>

Hi all,

I’m trying to create a named query that will only pull data on items that have been introduced after a certain date (this date happens to be whatever the current date is - 7 days). I have been having a lot of trouble getting date functions to work. After a lot of headaches I found that the following function seems to work to acquire the current date:

DECLARE @Date\_Value DATETIME  
SET @Date\_Value = GETDATE()

The trouble I’m having now is that A: I don’t know the format that this date is in  
B: I don’t know how to subtract 7 days from the current date.

If anyone could help me out, I would appreciate it greatly. Ready to smash my head through a wall as I am not very Jython savvy.

Thanks,

Ethan

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 1, 2019, 5:36pm UTC](https://forum.inductiveautomation.com/t/ms-sql-queries-getting-current-date-and-using-date-arithmetic-to-filter-database-results/23755/2 "2019-04-01T17:36:27Z")

</div>

Something like this in your query should work.

```sql
SELECT *
FROM table
WHERE t_stamp >= DATEADD(day,-7, GETDATE())

```

---

<div class="post-metadata">

**Author:** ![EEdmondson](https://avatars.discourse-cdn.com/v4/letter/e/41988e/32.png) [@EEdmondson](https://forum.inductiveautomation.com/u/EEdmondson)\
**Post date:** [April 1, 2019, 5:55pm UTC](https://forum.inductiveautomation.com/t/ms-sql-queries-getting-current-date-and-using-date-arithmetic-to-filter-database-results/23755/3 "2019-04-01T17:55:31Z")

</div>

You’re a life saver @JordanCClark. Worked like a charm.
