Remove Phone Numbers & Email Addresses in a string SQL - sql

I have a Table with Field "UserId" in Postgres
The List Contains a list of UserIds (comma separated) but also might contain email addresses and phone numbers .
Example
ID
UserID
1
11111,22222,+9199999999,xyz#yxz.com
3
2222,3333,11,+887777777,abc#bca.com
I want to remove all the phone-numbers and Email addresses and get the list of all userids comma separated in a new field.
OUPUT
ID
UserID
1
11111,22222
3
2222,3333,11
Also it will be better to have the query being optimised as it will be a part of a much complex query and should not not impact the performance .
Can someone suggest an ideal way to do it ?
Thanks
I have tried SUBSTRING , SPLITPART and Case Conditions based on it , but couldn't come out with a proper solution.

Use the REGEXP_REPLACE function to remove phone numbers.
UPDATE tablename SET columnname = REGEXP_REPLACE(columnname, '(\+[0-9]{1,3}[- ]?)?[0-9]{10,}', '');
Use the REGEXP_REPLACE function to remove email addresses.
UPDATE tablename SET columnname = REGEXP_REPLACE(columnname, '[A-Z0-9._%+-]+#[A-Z0-9.-]+\.[A-Z]{2,}', '');

Related

Update string rows by substring patterns sql

Got a apparently simple problem resolvable.
I want to update emails in my sql table with one query.
All emails ending by #notgoodexample.com (knowing I have several pattern that my email will match)
Should after that query be #goodexample.com
And I need to keep what is before the '#'.
My best try for the moment is
Update tableName SET table.email_addresse = concat(table.name, table.lastname, '#goodexample.com' --cheating a little bit cause these addresses are name.lastname#
WHERE email_address LIKE '%pattern%'
OR email_address LIKE '%pattern2%'
Do you have any tips for me ?

Teradata Character Column with non alphabet values

I have a name column in Teradata that has customer full name all in one column. There are some names with -,_,.,/,#,! in between the name characters. I want to be able to pull records where there are names with these conditions. Is there a better option to pull records with the scenario below?
Currently, I am writing query like this
SELECT NAME FROM TABLESOURCE WHERE NAME LIKE ANY('%-%','%.%','%#%','%~%','%!%')
Thanks in advance.
I haven't tested this but I think you could test for equality when those characters are removed from the name using otranslate
select name
from tablesource
where name <> otranslate(name,'-.#~!','')

postgresql - check if a row contains a string without considering spaces

Is it possible to check if a row contains a string without conisdering spaces?
Suppose I have a table like the one above. I want to know if the query column contains a string that may have different consecutive number of space than the one stored or vice versa?
For example: the first row's query is select id, username from postgresql, and the one I want to know if stored in the table is:
select id, username
from postgresql
That is to say the one that I want to know if exists in the table is indented differently and hence has different number of space.
You can use REGEXP_REPLACE; this will likely be very slow on large data set.
SELECT * from table
where REGEXP_REPLACE('select id, username from postgresql ', '\s+$', '') = REGEXP_REPLACE(query, '\s+$', '')
I think you would phrase this as:
where $str ~ replace('select id, username from postgresql', ' ', '[\s]+')
Note: This assumes that your string does not have other regular expression special characters.

SQL removing part of delimited field based on joining match?

Maybe there is a better method before I get to this step, but is there an easy way to match on one field, if it matches remove part of the match from a string in a second field.
TABLE example
ID | ID LIST
-----|---------
ID07 |ID05;ID06;ID07;ID08
This is just a one record example so ID and ID LIST will vary.
I'm looking to join and update/ replace the "nothing" or perhaps add a value to remove later.
Result I'm looking for
ID | ID LIST
-----|---------
ID07 |ID05;ID06;ID08
Is there any easy way to do this or should I go about this another way? I know some people would use a WHERE IN, but ID is going to vary. Maybe WHERE IN that field name. I'm a little confused conceptualizing this.
I'm using SQL Server MGMT studio.
You can use replace function .. if id is in id_list is replaced with empty string
select replace(ID_LIST, ID +';', '')
from your_table;
UPDATE TABLE
SET ID_LIST = CASE WHEN ID_LIST = ID THEN ''
WHEN ID_LIST LIKE ID + ';%' THEN SUBSTRING(ID_LIST, LEN(ID)+1, LEN(ID_LIST)-LEN(ID)-1)
WHEN ID_LIST LIKE '%;' + ID THEN LEFT(ID_LIST, LEN(ID_LIST)-LEN(ID)-1)
ELSE REPLACE(ID_LIST, ';'+ID+';', ';')
END
WHERE ';'+ID_LIST+';' LIKE '%;'+ID+';%'

split string with specific character

Here is my situation
there is a column that comes from csv file, i loaded all fields properly in sql but in email column, i have more than 1 email address for some records.
if it would be 2 emails, i could handle but as long as i have 3-4 or more emails, then i have little problem.
here is an example
'ekuntsche#addictionsuisse.ch;ekuntsche#suchtschweiz.ch;ekuntsche#sfa-ispa.ch;ekuntsche#addiction-info.ch'
this is only 1 column, i need to split them to 4 different column as email1,email2,email3,email4 and delimiter is ';'
I think i should use charindex and substring etc. but i could not create it.
if someone can create a function and show how to run the function, that would be super helpful.
thank you
database name 'bi_deploy'
main column name is 'email'
unique id for this table is 'ID'
(this is only email table, every row will have 1 email address, i can make it actually, but i could not split emails properly)