I am using Sqlite.
And I have table T with three columns: ID (int), Username (Text) and Message (Text).
I need something like this:
SELECT * FROM T WHERE Message CONTAINS <URL>
Is there a way to achieve this?
I read that Sqlite has function for handling regular expressions (regexp) but it must be implemented manually. And at this point implementing REGEXP is not an option.
The REGEXP operator is a special syntax for the regexp() user function. No regexp() user function is defined by default and so use of the REGEXP operator will normally result in an error message. If an application-defined SQL function named regexp is added at run-time, then the X REGEXP Y operator will be implemented as a call to regexp(Y,X).
try this :
SELECT * FROM T WHERE Message LIKE '%URL%';
Related
I applied advance filter in PowerBI desktop, It is working. Similarly, I want to apply "Contains" or "In" operator to filter a report using URL Query String Parameter. I want to filter with delimitated value. "eq" operator is working but "in/contains" operator is not working.
I tried like this:
lstReportFilter.Add(string.Format("&filter=Sheet1/TPId in |181|"));
I have taken reference from here: https://learn.microsoft.com/en-us/power-bi/collaborate-share/service-url-filters
Can you please correct me on what I am doing wrong here?
UPDATE:
I also tried with round bracket, but still its not working. The column have data with pipes, so I need to include pipe while filtering.
lstReportFilter.Add(string.Format("&filter=Sheet1/TPId in (|181|)"));
lstReportFilter.Add(string.Format("&filter=Sheet1/TPId in ('|181|')"));
PowerBI column data
IN Operator expects a comma seperated list of primitive values or a expression
eg:
~/Products?$filter=Name in ('Milk', 'Cheese')
~/Products?$filter=Name in RelevantProductNames
~/Products?$filter=ShipToAddress/CountryCode in MyShippers/Regions
~/Products?$filter=Name in Fully.Qualified.Namespace.MostPopularItemNames
It seems like your use case is the first example, but if you don't have multiple values you can simply use the EQ Operator
like: filter=Sheet1/TPId eq |181|
Reference:
https://learn.microsoft.com/odata/webapi/in-operator
https://learn.microsoft.com/power-bi/collaborate-share/service-url-filters
I am using REGEXP_EXTRACT function in Google BigQuery to extract a specific word from a string. While regexp works good when tested, function REGEXP_EXTRACT returns null in Google BigQuery.
For example, there is string "RR_SM_Brand_A_Additive_Clean_jun2020", and I want to extract a value from the list (Brand_A, Brand_B, Brand_C, etc.)
When I test RegExp, I receive correct value Brand_A: https://regexr.com/5tecm
RegExp Code: Brand_A|Brand_B (thanks to #Barmar)
But when I run it in Google BigQuery:
SELECT distinct utm_campaign, -- REGEXP_EXTRACT(utm_campaign, r"(?:Brand_A|Brand_B)") REGEXP_EXTRACT(utm_campaign, r"Brand_A|Brand_B") FROM project.dataset.table WHERE utm_campaign = "RB_Display_Brand_A_Botanica_2020"
I receive "This query returned no results.", and not expected Brand_A value.
Note: BigQuery does not return "Cannot parse regular expression: invalid perl operator: (?<"." like in question "duplicate"
I'd suggest that your WHERE clause may be at issue. Both forms you use extract the brand string you appear to be asking for. If the REGEXP_EXTRACT was not matching you'd still get rows, but the value would be NULL.
Converting this to just use the literal from your existing where clause:
SELECT
val,
REGEXP_EXTRACT(val, r"(?:Brand_A|Brand_B)"),
REGEXP_EXTRACT(val, r"Brand_A|Brand_B")
FROM
(
SELECT "RB_Display_Brand_A_Botanica_2020" as val
)
Our Django application just migrated from MySQL to PostgreSQL, and we are in the process of updating our user queries to use the correct syntax. These queries are stored/executed via Django-SQL-Explorer.
I have a query that for simplicity's sake looks like this:
SELECT * FROM table t WHERE t.scheduled_start_date BETWEEN $$from_date$$ AND $$to_date$$
The query above works, but I would like to set defaults for today & today+30 respectively.
I've tried the following WHERE clauses to no avail:
Works with user entered date, default throws syntax error
WHERE t.scheduled_start_date BETWEEN date('$$from_date:CURRENT_DATE$$') AND date('$$to_date:CURRENT_DATE + INTERVAL \'30 DAY\'$$')
Error using defaults:
syntax error at or near "30" LINE 28: ...urrent_date') AND date('current_date + interval \'30 day\'') ^
Defaults work correctly, but user entered dates do not:
WHERE t.scheduled_start_date BETWEEN date($$from_date:CURRENT_DATE$$) AND date($$to_date:CURRENT_DATE + INTERVAL '30 DAY'$$)
Error with user dates:
function date(integer) does not exist LINE 28: WHERE t.scheduled_start_date BETWEEN date(2019-09-30) AND da... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
The message about casting makes sense to me since the user enters in string data, however I can't seem to get the syntax right that casts to DATE but only when the default is overridden.
Any suggestions would be helpful. Thanks!
Your input is a string value, current_date is a function call. The former needs quotes whereas the latter does not. Therefore your client side substitution will not work. You can move the default logic into the SQL statement using the COALESCE() function doc.
In case the client sends NULL values for an empty user input this would result in the following where clause:
BETWEEN date(COALESCE('$$from_date$$',current_date))
AND date(COALESCE('$$from_date$$',current_date + interval '30 DAYS'))
You added that your client sends an empty string, not a NULL value. So this should be changed to:
BETWEEN COALESCE(NULLIF('$$from_date$$','')::date, current_date)
AND COALESCE(NULLIF('$$from_date$$','')::date, current_date + interval '30 DAYS')
The PostgreSQL's regular expressions are expressed as strings (text datatype),
SELECT regexp_matches('foobarbequebaz', '(bar)(beque)'::text);
so is natural to imagine it as a dynamic parameter... But no, is not possible to be dynamic... This query "failed to find conversion function from unknown to text",
SELECT regexp_matches('foobarbequebaz', (SELECT '(bar)(beque)') );
... So, we can imagine something intermediary to reuse regular expressions, as pre-compile it. It is possible with PostgreSQL v10?
The error message has nothing to do with "dynamic" values or "precompiled" values. It simply tells you that the result of (SELECT '(bar)(beque)') has an unknown data type, but regexp_matches() expects text
So you need to cast that result to text:
SELECT regexp_matches('foobarbequebaz', (SELECT '(bar)(beque)')::text );
If you want to pass the expressions from somewhere else, you can do it like this:
with list_of_expressions (expression) as (
values
('(bar)(beque)'),
('(foo)')
)
SELECT regexp_matches('foobarbequebaz', expression)
from list_of_expressions;
Of course list_of_expressions could also be a table in your database.
like clause doesn't want to fetching all the results that near to my word, I must write the complete sentence to get it, for instance:
I have the following data in database:
Lion king
lionheart
level completed
good morning
I want to fetch Lion king, and when I write this part of word lion, in normal it must fetch Lion king and lionheart, but in my case does not fetch anything, unless, must write the complete sentence to fetch the data.
I tried to use the following queries:
SELECT * FROM table WHERE name LIKE '%text'
SELECT * FROM contacts WHERE name LIKE '%text' OR name LIKE 'text%' OR name LIKE '%text%' OR name LIKE text
Notice:
I'm use C++ Qt Framework and the following is what I did
qry.prepare("SELECT * FROM table WHERE name LIKE '%:text'");
qry.bindValue(":text", ui->searchBox_txt->text());
qry.exec();
How can I make the query to does the normal behavior ?
:text
is a query parameter whose name is text
':text'
is the string ":text"
Similarly, "%:text" is exactly what it is; your parameter isn't parsed because it's inside of a string.
You need to concatenate the parameter in with your '%' using the concatenation operator (||):
SELECT * FROM table WHERE name LIKE '%' || :text || '%'
Remove the : after the %. Something like this:
SELECT * FROM table WHERE name LIKE '%text%'
Please also take note that the value is case sensitive