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

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.

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.

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

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

SELECT GETDATE() AS CurrentTime, other_columns 
FROM your_table

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

2 Likes