Getting all the occurrences of a substring within a string - sql

How can I get all the occurrences of a substring within a string in PostgreSQL?
I have this string for an ID:
BS Score xxxxxxx075SCxxxBS Score xxxxxxx062SCxxxBS Score xxxxxxx115SCxxx
And I would like to get the numbers in the string for the ID, so the result can look like this:

You can use regexp_matches:
select id intl, regexp_replace(v[1], '^0+', '') values from tbl
cross join regexp_matches(id, '(\d+)', 'g') v
See fiddle.

Related

redshift regexp_substr all occurrences

I am trying to return all results that match a specific regex in Redshift database.
Sample query
WITH aa AS
(SELECT DISTINCT
id,
record,
regexp_substr(record, '(#{2})([A-Z]{2,3})',1,1),
REGEXP_COUNT(record, '(#{2})([A-Z]{2,3})')
FROM table_a)
SELECT
*
FROM aa
The above query returns the first occurrence and the total count of the occurrences.
Is there a way to return all occurrences? Maybe an iteration using a variable that is limited by the count?
regexp_substr(record, '(#{2})([A-Z]{2,3})',1,n)
There is a solution for Oracle with the CONNECT BY LEVEL, but that option seems to be unavailable for Redshift.
If I understand correctly, you can use split_part() instead:
split_part(record, '##', <n>)
Then you can attempt to generate numbers and get the results:
with n as (
select row_number() over () as n
from t
limit 10
)
select t.*, split_part(record, '##', n.n)
from t join
n
on split_part(record, '##', n.n) <> ''

substring query

I want to get the substring out of a cell value wrt following eg-
Input: "J.H.Ambani.School"-----------School
Output: "H.Ambani"-----------------MidName
That is all the text that comes between the first and the last dots. Length of string or number of dots in string can be any. I am trying to form a query for above input column "School" to get the output column "MidName".What can be the sql query for it?
For Oracle Database:
SELECT
REGEXP_REPLACE(yourColumn, '^[^.]*.|.[^.]*$', '') AS yourAlias
FROM yourTable
If is correctly understood your problem by your statement
"That is all the text that comes between the first and the last dots". Then below is solution to your problem is as given below. Below is working solution in SQL SERVER, for other databases i could not check because of lack of time.
#SourceString : this is your input
#DestinationString : this is your output
declare #SourceString varchar(100)='J.H.Ambani.School'
declare #DestinationString varchar(100)
;with result as
(
select ROW_NUMBER()over (order by (select 100))SNO,d from(
select t.c.value('.','varchar(100)')as d from
(select cast('<a>'+replace(#SourceString,'.','</a><a>')+'</a>' as xml)data)as A cross apply data.nodes('/a') as t(c))B
)
select #DestinationString=COALESCE(#DestinationString+'.','')+ISNULL(d,'') from result where SNO>(select top 1 SNO from result order by SNO)
and SNO<(select top 1 SNO from result order by SNO desc)
select #DestinationString

Building a string based on columns from a GROUP

I have a table like this:
user 1 A
user 1 B
user 2 H
user 2 G
user 2 A
and I need a result like:
user 1 AB
user 2 HGA
Is there a way to obtain a result like this?
So here we create some test data
CREATE TABLE foo AS
SELECT * FROM (
VALUES (1,'A'),(1,'B'),(2,'H'),(2,'G'),(2,'A')
) AS f(id,col);
This should work,
SELECT id, array_to_string(array_agg(col), '')
FROM table
GROUP BY id;
Here is what we're doing,
GROUP BY id.
Build a PostgreSQL text[] (text array) of that column with array_agg
Convert the array back to text by joining on an empty string '' with array_to_string.
You can also use string_agg,
SELECT id, string_agg(col, '')
FROM foo
GROUP BY id;
The better soluction is using str_sum agregate function
select
user,
str_sum(column_name,'')
from table_name
group by user;

oracle regexp_substr to fetch string

I have a sql query like
select count(distinct empno), count(distinct(deptno empname empid )) from emp
I want to fetch first occurrence of count and its content like:
count(distinct empno)
I have tried the following sql statement but its not working,
SQL >select regexp_substr('select count(distinct empno),
count(distinct(deptno)) from emp', 'count *( distinct .)')
from dual ;
REGEXP_SUBSTR('SELECTCOUNT(DISTINCTEMPNO),COUN
---------------------------------------------- count(distinct empno), count(distinct(deptno ))
I want output to return like :
REGEXP_SUBSTR('SELECTCOUNT(DISTINCTEMPNO)
count(distinct empno),
Regular expression should not match second ) closing bracket and it should match the 1st closing bracket.
* in your regex ates everything, you need to make the regex engine to match the shortest possibility.
count *\([^\)]\)(?=,)

How to use GROUP BY to concatenate strings in MySQL?

Basically the question is how to get from this:
foo_id foo_name
1 A
1 B
2 C
to this:
foo_id foo_name
1 A B
2 C
SELECT id, GROUP_CONCAT(name SEPARATOR ' ') FROM table GROUP BY id;
https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_group-concat
From the link above, GROUP_CONCAT: This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values.
SELECT id, GROUP_CONCAT( string SEPARATOR ' ') FROM table GROUP BY id
More details here.
From the link above, GROUP_CONCAT: This function returns a string result with the concatenated non-NULL values from a group. It returns NULL if there are no non-NULL values.
SELECT id, GROUP_CONCAT(name SEPARATOR ' ') FROM table GROUP BY id;
:-
In MySQL, you can get the concatenated values of expression combinations
. To eliminate duplicate values, use the DISTINCT clause. To sort values in the result, use the ORDER BY clause. To sort in reverse order, add the DESC (descending) keyword to the name of the column you are sorting by in the ORDER BY clause. The default is ascending order; this may be specified explicitly using the ASC keyword. The default separator between values in a group is comma (“,”). To specify a separator explicitly, use SEPARATOR followed by the string literal value that should be inserted between group values. To eliminate the separator altogether, specify SEPARATOR ''.
GROUP_CONCAT([DISTINCT] expr [,expr ...]
[ORDER BY {unsigned_integer | col_name | expr}
[ASC | DESC] [,col_name ...]]
[SEPARATOR str_val])
OR
mysql> SELECT student_name,
-> GROUP_CONCAT(DISTINCT test_score
-> ORDER BY test_score DESC SEPARATOR ' ')
-> FROM student
-> GROUP BY student_name;
The result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024 characters, so we first do:
SET group_concat_max_len=100000000;
and then, for example:
SELECT pub_id,GROUP_CONCAT(cate_id SEPARATOR ' ') FROM book_mast GROUP BY pub_id
SELECT id, GROUP_CONCAT(CAST(name as CHAR)) FROM table GROUP BY id
Will give you a comma-delimited string
Great answers.
I also had a problem with NULLS and managed to solve it by including a COALESCE inside of the GROUP_CONCAT. Example as follows:
SELECT id, GROUP_CONCAT(COALESCE(name,'') SEPARATOR ' ')
FROM table
GROUP BY id;
Hope this helps someone else