split string with specific character - sql

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)

Related

Remove Phone Numbers & Email Addresses in a string 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,}', '');

Derby database appending space when returning values

Using a Derby Database:
In my addresses table, I have address information, with a column for the zip code of type varchar(45).
If I query (select * from addresses where address_id = 1234) and look at the ZIP Column, it returns '95035'.
If I query (select ZIP from addresses where address_id = 1234) it returns '95035 ' (With a space at the end).
I noticed this on the zip column because I have a basic validator running on the addresses and it checks for extra spaces in the zip code, so I don't know how widespread this issue is for other columns and tables. Less than .02% of my rows are being affected by this, and if I delete the row and re-enter it with the same data it fixes the problem.
Can anyone tell me why this would be happening/how to fix it?

Concatinate part of two columns

select name,(name,1,3||phone_no,1,3)from users order by (name);
I'm writing a query for the above problem and it is showing error.
Write a to display user name and password. The password should be generated by concatenating first three characters of user name and first three numbers in the phone number and give an alias name as the password. Sort the result based on user name
remove extra parenthesis and use substring function
select name,substr(name,1,3)|| substr(phone_no,1,3) from users order by name

Stored procedure that takes list of string as input and insert missing rows then return them

I have a table Names
Id Name
+----+---------------+
1 John
2 Kate
3 Mark
I want to create a stored procedure that does the following:
1) take a list of names as string as an input parameter
I have done some researches about this but couldn't find the best way to do it. I will call the stored procedure from the entity framework in a C# application. I was thinking of passing the names in one string separated with a comma and the split them in the procedure. But can't figure out how this is done.
2) for each name in the list, if the name does not exist in the Name column, insert a new row for it.
How can I do a switch case if it exists and insert it if not
3) Select * rows that are in the input list.
After adding all the missing Names, I want to select all the names that were in the input list with their id
Can this be done in one stored procedure, or do I have to divide them into multiple.
I am looking for hints on how to do each step, and if they can be combined.
Thanks
Keep your DB side lean and leave logic on the app side, especially if you have grumpy DBA's.
Use a MERGE/Upsert instead.
Check out this SO post.
If you pass a comma delimited list into a stored procedure as a parameter, you are going to need to understand how to use charindex, left, substring and right
As you split out each name - you would add them into a temporary table or a table valued variable.
Your decision about whether to insert the new names into the Names table would be made using an exists() subquery on an insert statement.
You could then, finally, fashion a select statement to join your temp table/table valued variable back to your Names table to pull out all of the keys (including the new ones) and pass them back to your front end.

SQL Query: Modify records based on a secondary table

I have two tables in a PostgreSQL database.
The first table contains an ID and a text field with up to 200 characters and the second table contains a data definition table which has a column that contains smileys or acronyms and a second column which converts them to plain readable English.
The number of records in table 1 is about 1200 and the number in table two is about 300.
I wish to write a SQL statement which will convert any text speak in column 1 in table one into normal readable language based on the definitions in Table 2.
So for example if the value in table 1 reads as: Finally Finished :)
The transformed SQL would be something like: Finally Finished Smiles or smiling,
where the definition is pulled from the second table.
Note the smiley could be anywhere in the text in column one and could one of three hundred characters.
Does anyone know if this is possible?
Yes. Do you want to do it entirely in SQL, or are you writing a brief bit of code to do this? I'm not entirely sure of how to do it all in SQL but I would consider something like what is below:
SELECT row.textToTranslate FROM Table_1
oldText = row.textToTranslate
Split row.textToTranslate by some delimeter
For each word in row.textToTranslate:
queryResult = SELECT FROM Table_2 WHERE pretranslate=word
if(queryResult!=Null)
modifiedText = textToTranslate.replace(word, queryResult)
UPDATE Table_1 SET translatedText=modifiedText WHERE textToTranslate=oldText