_Jim
January 20, 2022, 4:06pm
1
I have a db table with records and one field is a timestamp.
Now I would like to filter the query with only those records that are more than a week old.
My idea/approach would be to execute a small calculation:
get the current timestamp minus 604,800 (total seconds in a week) and save that in a variable
Then I could use that variable in the condition: WHERE datetime < $variable
Is this possible?
Or is there a better way?
sid
January 20, 2022, 5:06pm
2
Hi.
You could just use the NOW binding with date add formatter and put - 7 days as input.
This would remove the need for extra variable and calculations. Although, that method seems fine as well.
use the SQL databaseās own NOW() function - fastest and avoid any local timezone drift if your not using UTC throughout
_Jim
January 20, 2022, 5:55pm
4
Hi Sid!
Sounds great. Is this possible from within the query builder?
Can you show me how?
Also in the equation field there is no āgreater thanā or āsmaller thanā option that I can find⦠Is there another way?
_Jim
January 20, 2022, 5:56pm
5
Hi Scalaris. (How) can this be done from within the query builder?
No. Apologies, I tend to use a lot of custom queries these days and native SQL.
For query builder, use a date variable in your SQL and bind it to a server action variable with value NOW (NOW_UTC) - 7 days
NOW_UTC.dateAdd(ādaysā, -7)
_Jim
January 20, 2022, 6:10pm
7
Ha. Well that is exactly what I am looking for. But where can I find/set the server action variable?
In your server action, click the + field and then search for āSet ā¦ā. Select āSet valueā icon when this appears:
_Jim
January 20, 2022, 6:42pm
9
Great, found it!
But now the equation in the builder. It has no āsmaller thanā option:
_Jim
January 20, 2022, 6:48pm
12
Also in my custom query I get an error using the value.
Did I do it wrong?
WOW. Thatās an omission and a half. Is it configured as a date field ? Maybe itās a recent build thatās omitted it ??
@Teodor - any ideas why itās missing ?
Hereās mine - older version of wappler . 3.9.x
The data type is text- change to date.
Iād also use a :Parameter rather that {{session_tmdt}}
E.g. :P1 and set that to the variable.
Try that
Iād also say that using the sql names id and email should be wrapped as āidā, āemailā as they MAY be reserved words in MariaDB. I donāt know that server at all.
If youāre using a custom query, you donāt need the :Parameter. Just use:
DATE_SUB(NOW(), INTERVAL 7 DAY);
1 Like
_Jim
January 20, 2022, 7:21pm
16
Alright, I see the field was set as string. If I choose another field that is set as interget the < > options are there!
Cool. As I said, as youāre using a custom query anyway, Iād use the code above directly in the SQL.
_Jim
January 20, 2022, 7:40pm
18
Well I started building the query in the custom query because I could not find the āLess thanā option
Make it work both ways. Itās all good learning. Wappler has quite a steep curve but itās worth it. I assure you.
1 Like
_Jim
January 20, 2022, 9:40pm
20
Scalaris, can you tell me what DATE_SUB(NOW(), INTERVAL 164 DAY) translates into?
My datetime field is a timestamp so I guess I need this function to first turn into a timestamp aswell.
Any idea how?