# Problem with $\_\_timeFilter() MSSQL

**URL:** <https://community.grafana.com/t/problem-with-timefilter-mssql/131896>\
**Category:** Dashboards\
**Tags:** time-selection\
**Created:** [September 12, 2024, 7:57am UTC](https://community.grafana.com/t/problem-with-timefilter-mssql/131896 "2024-09-12T07:57:17Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![amrasine](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@amrasine](https://community.grafana.com/u/amrasine)\
**Post date:** [September 12, 2024, 7:57am UTC](https://community.grafana.com/t/problem-with-timefilter-mssql/131896/1 "2024-09-12T07:57:17Z")

</div>

Hello,

I’m using Grafana 11.0.0 with an MSSQL database

I have a problem with the use of $\_\_timeFilter(), in fact it doesn’t give the right date at all to filter the data no matter what time zone you choose. There’s always a 2h offset (I’m in UTC+2).  
Here are 3 identical requests (except one where I add the $\_\_timefilter()) and the results are not identical:

Request in MSSQL :  
 ![image](https://us1.discourse-cdn.com/grafana/original/3X/7/b/7b1946206b1456c1085237a74e40c44a46c1bce0.png)

Requests in Grafana with UTC+0 :

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/a/f/afc6926ada056df434ecb90074cf19a98f3a148b.png)  
Requests in Grafana with UTC+2 :  
 ![image](https://us1.discourse-cdn.com/grafana/original/3X/2/a/2a9101fc5da2e99e97b91b05e39a5a8a4904f886.png)

The correct result is obviously on MSSQL, but I don’t understand why the $\_\_timeFilter() has such an effect on the result. It’s impossible to get the right result, and even worse when we just look at the date by applying UTC+2 in time zones Grafana makes us think we’re smme at the right date when in fact it’s only modifying the date imported from the database but not actually fetching the data at that date.

How we can fix this plz ?

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [September 12, 2024, 11:18am UTC](https://community.grafana.com/t/problem-with-timefilter-mssql/131896/2 "2024-09-12T11:18:01Z")

</div>

grafana requires your datetime fields to be utc. if not use functions that make the date time UTC. what time zone are you in? `Africa/Ethiopia` etc

```auto
SELECT CONVERT(DATETIME,
                @dt AT TIME ZONE 'Central European Standard Time'
                    AT TIME ZONE 'UTC');

```

---

<div class="post-metadata">

**Author:** ![amrasine](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@amrasine](https://community.grafana.com/u/amrasine)\
**Post date:** [September 13, 2024, 5:42am UTC](https://community.grafana.com/t/problem-with-timefilter-mssql/131896/3 "2024-09-13T05:42:59Z")

</div>

Thank you for your answer, it’s really not practical …, ed plus I couldn’t find a way to apply this conversion to the $\_\_timeFilter() macro, I had to use $\_\_timeFrom() AND $\_\_timeTo()… $\_\_timeFilter() is only useful if you’re in UTC, otherwise you might as well do something else…

---

<div class="post-metadata">

**Author:** ![joachimschiewek](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/joachimschiewek/32/38184_2.png) [@joachimschiewek](https://community.grafana.com/u/joachimschiewek)\
**Post date:** [September 13, 2024, 12:42pm UTC](https://community.grafana.com/t/problem-with-timefilter-mssql/131896/4 "2024-09-13T12:42:09Z")

</div>

what happens if you crack the timeFilter macro with…

when  
starttime BETWEEN $\_\_timeFrom() AND $\_\_timerTo()

??

---

<div class="post-metadata">

**Author:** ![amrasine](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@amrasine](https://community.grafana.com/u/amrasine)\
**Post date:** [September 13, 2024, 12:57pm UTC](https://community.grafana.com/t/problem-with-timefilter-mssql/131896/5 "2024-09-13T12:57:18Z")

</div>

By selecting my time zone in the time picker, and replacing $\_\_timeFilter(date) with Date AT TIME ZONE ‘Central European Standard Time’ BETWEEN $\_\_timeFrom() AND $\_\_timeTo() it works correctly. But it’s a pity you can’t use $\_\_timeFilter easily if you’re not in UTC+0.

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [September 13, 2024, 1:28pm UTC](https://community.grafana.com/t/problem-with-timefilter-mssql/131896/6 "2024-09-13T13:28:24Z")

</div>

you can just use cte to convert the data before applying that function

```auto
;with utc_converted
as
(
select id, 
CONVERT(DATETIME,
starttime AT TIME ZONE 'Central European Standard Time' 
AT TIME ZONE 'UTC') as starttime 
from xys
where equipment = 'sdsds'
)
select top 1 id, starttime  
from utc_converted
where$__timeFilter(starttime )
order by date desc

```

not for copy pasta

---

<div class="post-metadata">

**Author:** ![amrasine](https://avatars.discourse-cdn.com/v4/letter/a/b5ac83/32.png) [@amrasine](https://community.grafana.com/u/amrasine)\
**Post date:** [September 13, 2024, 1:45pm UTC](https://community.grafana.com/t/problem-with-timefilter-mssql/131896/7 "2024-09-13T13:45:11Z")

</div>

Thanks for the tip 🙂 , but this need to adapt the date to use $\_\_timeFilter is a shame but at least I have a solution

---

<div class="post-metadata">

**Author:** ![solitcon](https://avatars.discourse-cdn.com/v4/letter/s/8e7dd6/32.png) [@solitcon](https://community.grafana.com/u/solitcon)\
**Post date:** [September 13, 2024, 1:59pm UTC](https://community.grafana.com/t/problem-with-timefilter-mssql/131896/8 "2024-09-13T13:59:05Z")

</div>

it would be a shame for that function to try to account for all the possible time zones. thats why utc is the preferred standard everyone can agree on

---

<div class="post-metadata">

**Author:** ![lbertholdopereira](https://avatars.discourse-cdn.com/v4/letter/l/0ea827/32.png) [@lbertholdopereira](https://community.grafana.com/u/lbertholdopereira)\
**Post date:** [September 13, 2024, 3:29pm UTC](https://community.grafana.com/t/problem-with-timefilter-mssql/131896/9 "2024-09-13T15:29:53Z")

</div>

Bom dia pessoal, sou novo no grafana, alguém poderia me ajudar em como deixa o grafana filtrando assim ?

 ![Captura de tela 2024-09-13 122920](https://us1.discourse-cdn.com/grafana/original/3X/7/c/7c575f688eafa382680269e59198ff7258216cf4.png)

---

<div class="post-metadata">

**Author:** ![lbertholdopereira](https://avatars.discourse-cdn.com/v4/letter/l/0ea827/32.png) [@lbertholdopereira](https://community.grafana.com/u/lbertholdopereira)\
**Post date:** [September 13, 2024, 3:32pm UTC](https://community.grafana.com/t/problem-with-timefilter-mssql/131896/10 "2024-09-13T15:32:38Z")

</div>

Ja tenho a query, porem não consigo fazer aparecer esse grafico ai

---

<div class="post-metadata">

**Author:** ![lbertholdopereira](https://avatars.discourse-cdn.com/v4/letter/l/0ea827/32.png) [@lbertholdopereira](https://community.grafana.com/u/lbertholdopereira)\
**Post date:** [September 13, 2024, 3:33pm UTC](https://community.grafana.com/t/problem-with-timefilter-mssql/131896/11 "2024-09-13T15:33:17Z")

</div>

SELECT FGO\_DH\_IMP, count(FGO\_SEQFGO)  
FROM EXTGOR  
WHERE FGO\_DH\_IMP \>= DATE\_SUB(now(), INTERVAL 5 HOUR)  
AND CONCAT(LPAD(HOUR(FGO\_DH\_IMP), 2, 0), ’ ', LPAD(MINUTE(FGO\_DH\_IMP), 2, 0)) \>= CONCAT(LPAD(HOUR(DATE\_SUB(CURTIME(), INTERVAL 1 HOUR)), 2, 0), ’ ', LPAD(MINUTE(DATE\_SUB(CURTIME(), INTERVAL 1 HOUR)), 2, 0))  
AND CONCAT(LPAD(HOUR(FGO\_DH\_IMP), 2, 0), ’ ', LPAD(MINUTE(FGO\_DH\_IMP), 2, 0)) \<= CONCAT(LPAD(HOUR(CURTIME()), 2, 0), ’ ', LPAD(MINUTE(CURTIME()), 2, 0));

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [September 13, 2024, 5:00pm UTC](https://community.grafana.com/t/problem-with-timefilter-mssql/131896/12 "2024-09-13T17:00:54Z")

</div>

Closed on September 13, 2024, 5:00pm UTC



---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [September 13, 2024, 5:01pm UTC](https://community.grafana.com/t/problem-with-timefilter-mssql/131896/13 "2024-09-13T17:01:23Z")

</div>

por favor abra um novo tópico.
