use a regex capture in a pgSQL select - sql

I've never used a regex in SQL. If I capture something like this in javascript, how can I refer back to the capture in pgSQL like I can with the 2nd element in javascript match() method?
var str = 'thomas-var1="SOME VAL1" thomas=var2="SOME VAL2" thomas-var-3="the value i want" thomas-var-4="SOME_VAL4"';
var re = /thomas-var-3="(.+?)"/i;
var new_str = str.match(re);
console.log(new_str[1]);
How can I put that into a SELECT statement so that I can say something like, retrieving the value "the value i want" from thomas-var-3?
SELECT * FROM forms WHERE name LIKE '%bill%' AND category = MY REGEX CAPTURE
rendering so something like"
SELECT * FROM forms WHERE name LIKE '%bill%' AND category ='the value i want'

you can use not regex, but hstore extension for this, like:
str := 'thomas-var1="SOME VAL1" thomas-var2="SOME VAL2" thomas-var-3="the value i want"'
str := replace(replace(str, '=', '=>'), '" ', '", ')
select *
from forms
where name like '%bill%' and category = hstore(str)->'thomas-var-3'

Using a subselect and the substring method you should be able to achieve what you want like so:
SELECT *
FROM firstTable
WHERE parentCat = (SELECT cat
FROM secondTable
WHERE cat in substring(column_to_capture_from from 'thomas-var-3="(.+?)' ));
from http://www.regular-expressions.info/postgresql.html
If there is a match, and the regex has one or more capturing groups, the text matched by the first capturing group is returned.

Related

Multi keywords search with SQL

I'm using a search bar on my website based on keyup binding and ajax requests. It works fine but I would like my search engine to be able to have finner result with multi keywords management.
However I could not find any simple method to set up this kind of search method.
Does anyone knows how to set this up ?
Here is the actual SQL request that's being made:
if ($recherche !=""){
$req = $this->bdd->prepare("SELECT * FROM videos WHERE titre LIKE :recherche OR auteur LIKE :recherche UNION SELECT videos.id_video, videos.titre, videos.lien, videos.auteur, videos.date_upload FROM videos RIGHT JOIN mots_clefs ON videos.id_video = mots_clefs.id_video AND mots_clefs.mot_clef LIKE :recherche ORDER BY date_upload DESC LIMIT ".$start.", ".$limit);
$req->execute(array('recherche' => "%".$recherche."%"));
$result = json_encode($req->fetchAll(PDO::FETCH_ASSOC));
}
Request example:
SELECT * FROM videos WHERE titre LIKE '%word 1 word 2%' OR auteur LIKE '%word 1 word 2%' UNION SELECT videos.id_video, videos.titre, videos.lien, videos.auteur, videos.date_upload FROM videos RIGHT JOIN mots_clefs ON videos.id_video = mots_clefs.id_video AND mots_clefs.mot_clef LIKE '%word 1 word 2%' ORDER BY date_upload DESC LIMIT 0, 20);
You can execute the above query in a loop by passing one keyword at a time.
get the entire keyword list provided by the user into a sting.
Use string.Split() method by passing comma(,) as delimiter to get the list of the keywords into an array list.
loop through the array list and pass to the query.
Make sure you append the data fetched from the SQL into a data table or dataset and Not rewrite it.
string select = "SELECT * FROM [MyTable] WHERE [Title] LIKE '%" + strSearch.Replace(",", "%' OR [Title] LIKE '%") + "%'";

LIKE with integers in PostgreeSQL using R

I need to download a table from postgree to R, but filtered by part of an INT.
I have been trying:
library(RPostgreSQL)
con <- dbConnect(PostgreSQL(), user= "#####", dbname="######",password="#####"
,host="#####", port='######')
vetor_id <- c("83052407","10406587","12272377")
match_id <- dbGetQuery(con,paste("
SELECT *
FROM public.data2015
WHERE id IN ('", paste(vetor_id,collapse = "','"),"')
",sep = ""))
dbDisconnect(con)
I also tried CONTAINS but didn't work.WHERE Contains(id,", paste(vetor_id,collapse = " OR "),"')
id is INT and vetor_id is just part of the values. I mean,vector_id = 83052407 must find id = 83052407000132.
How can I use something like LIKE and put vetor_id% ?
Is this what you want?
WHERE id::text like ? || '%'
This converts the integer id to a string, and attempts to match it against the parameter. If id starts with the parameter, the condition is satisfied.
Note that this uses a legitimate query parameter (represented by the question mark): you should get used to parameterize your queries rather than concatenating variables in the query string: this is cleaner, more efficient and safer.

Query to check if we have any row which start with input String

lets say have column with values
"Html", "Java", "JQuery", "Sqlite"
and if user enters
"Java is my favorite programming language"
then result should be
1 row selected with value "Java"
because entered sentence starts with "Java"
but if user enters
"My application database is in Sqlite"
then query should return empty.
0 row selected
because entered sentence does not start with "Sqlite".
I am trying following query:
SELECT * FROM demo where 'Java' like demo.name; // work
but if enter long text then it fails
SELECT * FROM demo where 'Java is my favorite programming language' like demo.name; // Fails
I think you want something like this :
SELECT * FROM demo WHERE yourText LIKE demo.name || '%' ;
Just put the column name on the left side of the like operator, and concatenate a wildcard at the right side of your string keywork:
select * from demo where name like 'Java%' ; -- or like 'Sqlite%' etc
You can make it more generic like:
select * from demo where name like ? || %' ;
where ? is a bind parameter that represents the value provided by the user (you may also concatenate with the wildcard in your application before passing the parameter).
Concatenate your column with a wildcard:
SELECT * FROM demo where 'Java is my favorite programming language' like demo.name || '%';
You can make your query like this
Select * from SearchPojo where name GLOB '*' || :namestring|| '*'"
Not sure if I got the problem but ...
You can query these values (Html, Java, JQuery, Sqlite) put them in a collection, like List, then grab user's input and check it:
List<String> values ... // Html, Java, JQuery, Sqlite
for (String value : values) {
if (userInput.contains(value) {
// print
System.out.println("1 row selected with value \"Java\");
}
}
It supports cases where user type more then one option.

Apply like function on an array is SQL Server

I am getting array from front end to perform filters according that inside the SQL query.
I want to apply a LIKE filter on the array. How to add an array inside LIKE function?
I am using Angular with Html as front end and Node as back end.
Array being passed in from the front end:
[ "Sports", "Life", "Relationship", ...]
SQL query is :
SELECT *
FROM Skills
WHERE Description LIKE ('%Sports%')
SELECT *
FROM Skills
WHERE Description LIKE ('%Life%')
SELECT *
FROM Skills
WHERE Description LIKE ('%Relationship%')
But I am getting an array from the front end - how to create a query for this?
In SQL Server 2017 you can use OPENJSON to consume the JSON string as-is:
SELECT *
FROM skills
WHERE EXISTS (
SELECT 1
FROM OPENJSON('["Sports", "Life", "Relationship"]', '$') AS j
WHERE skills.description LIKE '%' + j.value + '%'
)
Demo on db<>fiddle
As an example, for SQL Server 2016+ and STRING_SPLIT():
DECLARE #Str NVARCHAR(100) = N'mast;mode'
SELECT name FROM sys.databases sd
INNER JOIN STRING_SPLIT(#Str, N';') val ON sd.name LIKE N'%' + val.value + N'%'
-- returns:
name
------
master
model
Worth to mention that input data to be strictly controlled, since such way can lead to SQL Injection attack
As the alternative and more safer and simpler approach: SQL can be generated on an app side this way:
Select * from Skills
WHERE (
Description Like '%Sports%'
OR Description Like '%Life%'
OR Description Like '%Life%'
)
A simple map()-call on the words array will allow you to generate the corresponding queries, which you can then execute (with or without joining them first into a single string).
Demo:
var words = ["Sports", "Life", "Relationship"];
var template = "Select * From Skills Where Description Like ('%{0}%')";
var queries = words.map(word => template.replace('{0}', word));
var combinedQuery = queries.join("\r\n");
console.log(queries);
console.log(combinedQuery);

SQL Query to Substitute value in the scanned results and update that field of Table

I have a 1 to many Organization: Users relationship.
I want to fetch the usernames of all User model of an Organization, capture a part of that username and append/substitute it with new value.
Here is how I am doing:
Form the raw SQL to Get the matching usernames and replace them with new value.
raw = "SELECT REGEXP_REPLACE($1::string[], '(^[a-z0-9]+)((\s[a-z0-9]+))*\#([a-z0-9]+)$', m.name[1] || '#' || $2) FROM (SELECT REGEXP_MATCHES($1::string[], '(^[a-z0-9]+)((\s[a-z0-9]+))*\#([a-z0-9]+)$') AS name) m"
Get the matching usernames and replace them with new value.
usernames: list of usernames retrieved from queryable
Repo.query(raw, [usernames, a_string])
Error I am getting
SELECT REGEXP_REPLACE($1::string[], '(^[a-z0-9]+)(( [a-z0-9]+))#([a-z0-9]+)$', m.name[1] || '#' || $2) FROM (SELECT REGEXP_MATCHES($1::string[], '(^[a-z0-9]+)(( [a-z0-9]+))#([a-z0-9]+)$') AS name) m [["tradeboox#trdbx18"], "trdbx17"]
{:error,
%Postgrex.Error{connection_id: 7222, message: nil,
postgres: %{code: :undefined_object, file: "parse_type.c", line: "257",
message: "type \"string[]\" does not exist", pg_code: "42704",
position: "137", routine: "typenameType", severity: "ERROR",
unknown: "ERROR"}}}
FYI: The username field of User model is of type citext
Once I get the replaced values, I want to update the User with something like
update([u], set: [username: new_values])
Any ideas on how to proceed with this?
`
There is no string type in PostgreSQL.
Function regexp_matches accepts as first parameter only text and it can't be array. So what you need to do is first change that type to text, then unnest($1::text[]) your array. Iterate over resulting set of rows with those regexp.
raw = "SELECT REGEXP_REPLACE(m.item, '(^[a-z0-9]+)((\s[a-z0-9]+))*\#([a-z0-9]+)$', m.name[1] || '#' || $2)
FROM (
SELECT item, REGEXP_MATCHES(item, '(^[a-z0-9]+)((\s[a-z0-9]+))*\#([a-z0-9]+)$') AS name
FROM unnest($1::text[]) AS items(item)
) m"
If I understand it correctly, you are trying to replace everything after # with some different string - if that is the case, then your regexp will put anything after spacebar into second element of matches array. You would need this instead: ((^[a-z0-9]+)(\s[a-z0-9]+)*).
If above is true, then you can do all that much easier with this:
SELECT REGEXP_REPLACE(item, '((^[a-z0-9]+)(\s[a-z0-9]+)*)\#([a-z0-9]+)$', '\1' || $2) AS name
FROM unnest($1::text[]) AS items(item)
Best practice however is to simply do replace in UPDATE statement:
UPDATE "User" SET
name = concat(split_part(name, '#', 1), '#', $2)
WHERE organization_id = $3
AND name ~* '^[a-z0-9]+(\s[a-z0-9]+)*\#[a-z0-9]+$'
It will split name by #, take first part, then append # and whatever is assigned to $2 (domain name I guess). It will update only rows that have organization_id matching to some id and have names matching your regexp (you can omit regexp if you want to change all names from organization). Make sure that table in actually named User, case sensitive, or remove double quotes to have case insensitive version.
I sadly do not know how to do this in your ORM.