String Convertion and Interpolation

I want to query an SQL data source using Flux, but date filters should be dynamic. So, I created a type time flux object and convert it into string and used it in the query. Still, it didn’t work out for “-7d”.

Essentialy, it should run as a task and read data from the source in defined periods .So, please share if there is a better way to write this.

_start = string(v: now()) //This works fine.
_start = string(v: 2021-05-01) //This works fine too.
_start = string(v: -7d) //This one doesn't.
query = "SELECT ... FROM ... WHERE date_field > '${_start}' "

I want to query an SQL data source using Flux, but date filters should be
dynamic. So, I created a type time flux object and convert it into string
and used it in the query. Still, it didn’t work out for “-7d”.

You need to ensure that whatever you pass to MS SQL (I assume you mean that
you are using Microsoft SQL Server) is a valid MS SQL query.

_start = string(v: now()) //This works fine.
_start = string(v: 2021-05-01) //This works fine too.
_start = string(v: -7d) //This one doesn’t.
query = "SELECT … FROM … WHERE date_field > ‘${_start}’ "

Maybe MS SQL has a date_sub() function similar to MySQL’s?

https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html

Antony.

Good point, I can do it in the sql instead of trying to get the date in flux.

Still, I wonder how to get the datetime for n days ago (e.g.: some_date - 7d) in Flux instead of the SQL.

  • Is there date_sub() function in Flux?
  • -7d is duration type and I made wild guesses like now() -7d, didn’t work out.

Thanks,

@Mert You can add or subtract durations to time values using the experimental.addDuration() function:

import "experimental"

_start = string(v: experimental.addDuration(d: -7d, to: now()))
query = "SELECT ... FROM ... WHERE date_field > '${_start}' "

This method will only work with duration values though.