I can't find this in the docs or here, so I would like to ask your help on this one.
I would like to filter my sql data with some sort of "WHERE IN" construct.
Let's say I have a table with "Allow selecting multiple rows" enabled. This table has a field 'myDate' in it and I have selected 3 rows.
I think I’m almost there, but it does not work yet. I’m using SQL Azure, so I think I need the WHERE myDate IN option. (in sql server the ANY operation only works when using a subquery)
What I did is (using the array in the query):
WHERE
myDate IN
(
{{ tblTest.selectedRow.data.map(row => moment(row.myDate).format("YYYY-MM-DD")) }}
)
And then I tried (using a composed string in the query):
WHERE
myDate IN
(
{{ tblTest.selectedRow.data.map(row => "'" + moment(row.myDate).format("YYYY-MM-DD") + "'" ).join(", ")}}
)
But I still get the error (in each case):
message:“Conversion failed when converting date and/or time from character string.”
Running the query in TSQL works, assuming that this is the statement that is pushed to the sql server, or at least, this is what works in TSQL:
WHERE
myDate IN ('2020-11-15', '2020-11-16', '2020-11-17')
By the way: is there a way to catch the SQL statement that is sent to the SQL server? I’ve had multiple occasions already where that would be very convenient.
WHERE
myDate
IN (
SELECT
convert(date, value)
FROM
string_split({{ tblTest.selectedRow.data.map(row => moment(row.myDate).format("YYYY-MM-DD")) }}, ',')
)
Interesting idea. We do that for our REST API / GraphQL integrations, but haven’t thought about doing it for SQL quite yet. There’s a lot less uncertainty with SQL statements - the only thing you wouldn’t get in the existing editor preview (when you hover over the statement) is prepared statements, so those little $1 things you see. What are you missing from the current setup?
Well, for example, if you have a look at this issue: Is this a bug? data undefined in selectedRow it's sometimes hard to debug.
My query returns no results there although I was under the impression that it should, given the parameter values and the query.
Not being able to see what statement is sent to the sql server (I just get 0 results in the Preview) makes it hard sometimes.
And sometimes I get data type errors (int vs string / datetime formats).
Oh, I found something now. After clicking preview and then in the query editor a message appears below the query window. Didn't know that!
Haha yes that’s what I was referring to! Clicking on any code block in Retool should give you that preview, which helps figure out what’s getting sent / run.