Extract text between parenthesis from a postgres table without creating additional column - regex

I am trying to extract text between parenthesis from a column in postgres table. I am using following command. It is creating an additional blank column.
SELECT *, SUBSTRING (col2, '\[(.+)\]') FROM table
My table looks like this:
col1 col2
1 mut(MI_0118)
2 mut(MI_0119)
3 mut(MI_0120)
My desired output is:
col1 col2
1 MI_0118
2 MI_0119
3 MI_0120
How can I extract the text without creating an additional column.
Thanks

Your regex is wrong, that's why you get an empty column. You don't want square brackets, but parentheses around the search string
select col1, substring(col2, '\((.+)\)')
from input
Online example

The * in the SELECT statement is including all columns. Then you are adding another unnamed column. If you do:
SELECT col1, SUBSTRING (col2, '\[(.+)\]') AS col2 FROM table
It will be closer to what you want.

Related

How to add leading Zero's to make location number in Power BI so it returns 3 digit #

Loc1= FORMAT('Table'[LOC],"000") Any suggestion would be helpful? The original Column LOC is a text field with returns 3 characters with leading Zero's. I need it in numeric form to be able to join and use it in calculated columns.
Found a work around to fix my problem, I came up with a solution to keep Loc column as text and add concatenate column with Business Unit ID and Loc & convert concatenated col to numeric field so I can use for filtering and joining.

Replace IDs in a table with actual values in DAX Power BI

I have two tables the first one contains a value and label combination. The second table contains a column containing IDs(matching with table 1 ) in cell separated by commas
Output : replace all IDs with respective values with commas intact
sample output

How to list the most frequent 3-word strings on Google Sheets

I have a list of industries with a adjacent list of industries to categorize them. I would like to know which industries are the most common but I don't manage to make Sheets interpret two-word categories as one.
First, I would like to know which 5 categories are the most common overall. Also I would like to know the top 5 one-word (black), two-word (red) and three-word (blue) categories.
Plus, I would like to get rid of the commas.
Here's what I want to achieve and a link to a google sheets document where I've laid out all the data:
https://docs.google.com/spreadsheets/d/13N8gc4POPhFhTvyqq-UugWS5GCgcONwliacSL8-MAr8/edit#gid=0
How can these categories be grouped and listed?
overal word:
=ARRAYFORMULA(QUERY(TRANSPOSE(SPLIT(QUERY(B3:B11&",",,99^99), ", ")),
"select Col1,count(Col1)
group by Col1
order by count(Col1) desc
limit 5
label count(Col1)''"))
overall phrase:
=ARRAYFORMULA(QUERY(TRANSPOSE(SPLIT(QUERY(B3:B11&",",,99^99), ",")),
"select Col1,count(Col1)
group by Col1
order by count(Col1) desc
limit 5
label count(Col1)''"))
one word:
=ARRAYFORMULA(QUERY(TRIM(TRANSPOSE(SPLIT(QUERY(B3:B11&",",,99^99), ","))),
"select Col1,count(Col1)
where not Col1 contains ' '
group by Col1
order by count(Col1) desc
limit 5
label count(Col1)''"))
two words:
=ARRAYFORMULA(QUERY(TRIM(TRANSPOSE(SPLIT(QUERY(B3:B11&",",,99^99), ","))),
"select Col1,count(Col1)
where Col1 matches '\w+ \w+'
group by Col1
order by count(Col1) desc
limit 5
label count(Col1)''"))
three words:
=ARRAYFORMULA(QUERY(TRIM(TRANSPOSE(SPLIT(QUERY(B3:B11&",",,99^99), ","))),
"select Col1,count(Col1)
where Col1 matches '\w+ \w+ \w+'
group by Col1
order by count(Col1) desc
limit 5
label count(Col1)''"))
Breaking the problem into 3 formulas will allow you to support as many "words" as you want.
Step 1) put formula in D29 treat all the words as a single word (looking at your question it seems like this is the only step you really need)
=query(arrayformula(trim(substitute(transpose(split(query({substitute(B3:B," ","_")},"select * where Col1 is not null",counta(B3:B)),", ")),"_"," "))),"select Col1, count(Col1) group by Col1 order by count(Col1) desc label Col1 'Descriptions', count(Col1) 'Frequency'")
Step 2) put formula in F29 Place this next formula next to the table produced by the formula above. D30:D should be replaced if you use different ranges.
=arrayformula({"Words";if(D30:D="","",1+LEN(D30:D)-len(SUBSTITUTE(D30:D," ","")))})
Step 3) put formula in G29 This will output the largest frequency ordered by word count D29:F should be replaced if you use different locations
=query({D29:F},"select * where Col1 is not null order by Col3,Col2 desc")
The advantage to doing it this way is you support 1,2,3,4... word frequency.

Create a new column by executing regular expression on existing column

I have column with data as follows:
p=Chicago, IL|q=rental houses
My goal is to obtain
Chicago IL rental houses as the outcome by running regular expression on the column via a select query.
Use below regx on string
/p=(.*)|q=(.*)/
Then join 2 substrings with spaces.
If you want get result from select query you can use select with concat or concat_ws function instead.

Validate column using regular expression in postgre SQL

I need to check whether a column in a table having a numeric value followed by decimal point and 3 precisions after the decimal point. Kindly suggest how to do using regular expression in postgre SQL (or) any other alternate method.
Thanks
The basic regex for digits, a period and digits is \d+\.\d{3}
You can use it for several things, for instance:
1. Add a Constraing to your Column Definition
ALTER TABLE mytable ADD (CONSTRAINT mycolumn_regexp CHECK (mycolumn ~ $$^\d+\.\d{3}\Z$$));
2. Find Rows that Don't Match
SELECT * FROM mytable WHERE mycolumn !~ $$^\d+\.\d{3}\Z$$;
3. Find Rows that Match
SELECT * FROM mytable WHERE mycolumn ~ $$^\d+\.\d{3}\Z$$;