PostgreSQL query to find a particular pattern in a text column - postgresql

I have a text column in my database table which contains values like
A/B
A/B/C
A/B/C/D
A/B/C/D/E
Now, I want to select only those rows where this column value contains maximum three occurrences of '/'.
For clarity - expected output should be:
A/B
A/B/C
A/B/C/D
Can anyone help me with such a query?

I think it would be easier to simply remove everything else and count the number of remaining characters:
where length(regexp_replace(the_column, '[^/]', '', 'g')) <= 3
Online example

Related

Copying contents of columns with Field calculator in Qgis

I have to split the content of a column into 2 differents columns using the QGIS Field Calculator. Basically, my table is something like that:
Basically I have to work with descriptio column omitting characters from 1-12 and then copy next 8 characters (in this case "AgilisSi") into the PresLACAGI column.
The other element to copy is the final number in descriptio column, ranging from 1 to 3 characters. Possibly the best is thing would be a syntax that reproduces in CodiClapa column the number after ": ", including the space in the syntax.
Thanks a lot!
Use the field calculator, check Update existing field and select column from drop down and type in the Expression window for:
PresLACAGI: substr(descriptio,12,8)
CodiClapa: right(descriptio,3)

Delete specific digit in a number using PostgreSQL

I have a column named membership_number varchar(255) in the memberships table.
Here is some sample data:
0000001234
0000002345
0000003456
membership_number must have 10 digits. If a user enters a number that has less than 10 digits, then the missing places are filled with leading zeroes accordingly.
At the moment, I have some data in this column as follows:
00001234.0
00002345.1
00003456.2
I would like to delete the decimal point which is the 2nd to the last digit and then add a leading zero to handle all of these undesired membership numbers. What would be the best way to do this?
I am aware of SUBSTRING() and its parameters but couldn't make it work so far.
Please backup your data before trying this.
Would that work?
UPDATE tablename SET membership_number = concat('0',replace(membership_number,'.','')) WHERE membership_number LIKE '%._'
Substring is not the function you want, you want a combination of REPLACE and LPAD functions:
select lpad( replace (membership_number, '.', ''), 10, '0')
from menberships;
(table name assumed) And why if it must be 10 digits do you define it as length up to 255?

How can I rename several columns in dataprep?

I have more than 100 columns in dataprep whose names are like:
my column name 1
my column name 2
I would like to rename the name of the columns to be:
my_column_name_1
my_column_name_2
I have tried to do a rename, changing " " by "_". However, dataprep only changes the first whitespace! Is there any way to change all the whitespaces?
Another question, when I do a function like rename, it is done just for a column. I can add more columns writing the name of de column. Is there any way to select all columns without writing all the names?
thank you so much!
You can shift-select multiple columns to Transform when the data is in column view mode.
Select the columns to apply to and then choose the transformation.
JSDBroughton answer did the trick for me although it's not so clear how to do it. Change your view to Columns (second icon from the left on the toolbar). Select the first column, then hold Shift and select the last column. You should now have all columns selected. Then right clock and select Rename. A new Recipe step will be added with all your columns already added. Then set the Option to "Find and replace".
In terms removing all the spaces I couldn't find any Cloud Dataprep pattern or Regular Expression which let me replace all my spaces in my columns. Having said that my columns had a maximum of 4 spaces so I simply added the same step multiple times. I used the Regular Expression \s to match spaces and I replaced them underscores.

Postgresql : How can I determine how many characters in a text

a column has type text and its data looks like "{U}{R}" or "{3}{U}{U}{U}".
How can I determine how many "U" contains this column?
I want to select those data who has at least one and at most three {U}.
You can remove the code U and compare the size before/after removing it. This difference is the number of occurrence.
select length('{3}{U}{U}{U}{R}{R}')-length(translate('{3}{U}{U}{U}{R}{R}','U','')) AS U_CNT;
--> 3
or more generaly
select length(colname)-length(translate(colname,'U','')) AS U_CNT;

Pulling a Substring which does not exist in the same position

I am looking to pull a substring from a Oracle database column using PL/SQL.
The column has a large string value which varies in size for different rows. Hence the substring I am referring to will not be in the same position for each row. But the substring is uniquely identifiable. It will be like ",RID!1455,". i.e. It will be preceded by a comma, have RID, followed by a !, followed by a number and then a comma.
I am interested in pulling this number followed by RID. Can you please help me with this. Thank you very much in advance
Consider below query:
Suppose table text1 has column text with string:
dfgggsdRID!3242dfgdfdg
Then below query will give you result '3242':
select substr(text, (select (INSTR(text, 'RID!', 1)+4) FROM text1),4) from text1;