# How to subtract current time from a other time unit in SQL?

**URL:** <https://community.grafana.com/t/how-to-subtract-current-time-from-a-other-time-unit-in-sql/137267>\
**Category:** MSSQL\
**Created:** [November 28, 2024, 10:43am UTC](https://community.grafana.com/t/how-to-subtract-current-time-from-a-other-time-unit-in-sql/137267 "2024-11-28T10:43:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohanagrawal](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@mohanagrawal](https://community.grafana.com/u/mohanagrawal)\
**Post date:** [November 28, 2024, 10:43am UTC](https://community.grafana.com/t/how-to-subtract-current-time-from-a-other-time-unit-in-sql/137267/1 "2024-11-28T10:43:34Z")

</div>

We are using Grafana for creating dashboard. We have data source as SQL Server. We have one time data in our SQL table. We can fetch it using query. We also want current date and time which we can fetch using a SQL query (If Grafana has option to get it directly, let me know.). Now I have two time fields e.g; 2024-11-28 12:00:00 and 2024-11-28 18:00:00. We want to find out the difference between them either in minutes or seconds. However the date could be of two different days too. Please share your insights. Thank you.

---

<div class="post-metadata">

**Author:** ![sowdenraymond](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/sowdenraymond/32/38541_2.png) [@sowdenraymond](https://community.grafana.com/u/sowdenraymond)\
**Post date:** [November 28, 2024, 11:58am UTC](https://community.grafana.com/t/how-to-subtract-current-time-from-a-other-time-unit-in-sql/137267/2 "2024-11-28T11:58:58Z")

</div>

which database are you using? most db’s have functions for date differences

- **SQL Server** : `DATEDIFF(unit, start_date, end_date)`
- **MySQL** : `DATEDIFF(end_date, start_date)`
- **PostgreSQL** : `EXTRACT(unit FROM age(end_date, start_date))`
- **Oracle Database** : `end_date - start_date`
- **IBM Db2** : `DAYS(end_date) - DAYS(start_date)`
- **SQLite** : The function is not standardized, but you can perform similar calculations using date and time functions.

---

<div class="post-metadata">

**Author:** ![mohanagrawal](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@mohanagrawal](https://community.grafana.com/u/mohanagrawal)\
**Post date:** [November 28, 2024, 2:05pm UTC](https://community.grafana.com/t/how-to-subtract-current-time-from-a-other-time-unit-in-sql/137267/3 "2024-11-28T14:05:19Z")

</div>

We are using SQL Server. Do we not have option in Grafana to get current time and do the subtraction?

---

<div class="post-metadata">

**Author:** ![grant2](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/grant2/32/42281_2.png) [@grant2](https://community.grafana.com/u/grant2)\
**Post date:** [November 28, 2024, 2:32pm UTC](https://community.grafana.com/t/how-to-subtract-current-time-from-a-other-time-unit-in-sql/137267/4 "2024-11-28T14:32:00Z")

</div>

> [@mohanagrawal](#):
>
> Do we not have option in Grafana to get current time

Not that I am aware of. In MS SQL, this should work:

```auto
SELECT GETDATE() AS CurrentTime, other_columns 
FROM your_table

```

and then follow the approach outlined by @sowdenraymond to do the subtraction.
