pull characters from string based on fixed character - sql

In an SQL query, I know how to use Left, Right and Mid but is there a way to pull values from a column leading up to a specific character in the column? Such as a column that looks like this:
testemail#test.com|Something Else|Error
I want everything from the left up to the first | but since this is an email address, there isn't a fixed value of characters in the address. I think this is easy but I just can't remember the function for this if there is one.
Thanks.

Try using this with substring and charindex:
Select substring([field],1,charindex('|',[field1]) -1 ) from table1
Replace field1 with your field.
CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] )
Searches an expression for another expression and returns its starting position if found.

Related

Keep substring that precedes an expression in SQLite

I want to split a varchar column on a certain expression and keep the left hand side of the result.
My column looks as follows:
varchar_col
keep_this__discard_this
keep_this_too__discard_this
I want to split all the strings on the double underscore ('__') and keep whatever comes before it. How can this be done in SQLite?
You can use:
select substr(varchar_col, 1, instr(varchar_col, '__') - 1)
Here is a db<>fiddle.

Regular expression for gettin data after - in sql

I have a column with assignment numbers like - 11827,27266,91717,09818-2,726252-3,8716151-0,827272,18181
Now i am selecting the records like
select assignment_number from table;
But now i want that the column detail is retreived in such a way that numbers are only retrieved without -2 -3 etc like
726252-3---> 726252 8716151-0-->8716151
I know i can use regex for this but i do not know how to use it
This will select everthing before the character -:
^([^-]+)
From 726252-3 will match 726252
You would use regexp() substr:
select regexp_substr(assignmentnumber, '[0-9]+')
This will return the first string of numbers encountered in the string.

replace two characters in one cell

I am using this query to replace one character in a cell
select replace(id,',','')id from table
But I want to replace two characters in a cell.
If the cell is having this data (1,3.1), and I want it to look like this (131).
How can I replace two different characters in one cell?
Use TRANSLATE instead of REPLACE(). It replaces each occurrence of a character in the first pattern with its matched character in the second. To remove characters, simply leave cut short the replacement string:
select translate(id, '1,.', '1') id from table
Note that the second string cannot be null. Hence the need to include 1 (or some other character) in both strings.
Find out more.
Obviously the more characters you need to convert/remove the more attractive TRANSLATE() becomes. The main use for REPLACE is changing patterns (such as words) rather than individual characters.
Can use
select replace(translate(id,',.',' '),' ','') from table;
or
select regexp_replace('1,3.1','[,.]','') from dual;
or
select replace(replace(id,',',''),'.','') from table;
Call the replace again.
select replace(replace(id,',',''), '.','') id from table
Do this:
select REPLACE(REPLACE(id,',',''),'.','')
Or use a regular expression:
select regexp_replace(id, '[.,]', '') id from table
Find out more

Splitting the full name and writing it to another table in SQL Server 2008

I have a table, say A, in which there is a column FULLNAME. Values stored under this column are in the format of "surname name middle_name" (with one space between each). And I have another table B, in which I have columns SURNAME, NAME and MIDDLENAME. What would be the best way to take all of the FULLNAME cells from the table A, split them accordingly and insert them into the table B?
Thanks
You can combine functions for searching an occurence in a string (which return normally its index) with the Substring function, besides you will need the Left and Right functions
For example in SQL Server you will find the functions:
CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] )
SUBSTRING ( expression ,start , length )
LEFT ( character_expression , integer_expression )
RIGHT ( character_expression , integer_expression )
STEPS:
Use the LEFT to get the 1st word (integer_expression = index of 1st
Emtpy space)
Use Substring to get the middle word (start is the index of 1st
Emtpy space + 1 , length is the entire length - the second index of
the emtpy space, use the startlocation to search the second occurence which should be the first occurence +1)
Use the right function to get the last word similar to step 1
Notice that if you have any names including empty spaces in the middle (example a first name like anna maria) this wouldnt work as expected.
This query will spilt your string.
select left(FULLNAME,CHARINDEX(' ',FULLNAME)), SUBSTRING(FULLNAME,CHARINDEX(' ',name)+1,len(FULLNAME)) from tableA

How do I modify these group of functions to get everything left of #?

I have a column that has values stored in the following format:
name#URL
All data is stored with this and a second # is never present.
I've got the following statement that strips the URL from this column:
SELECT SUBSTRING ( wf_name ,PATINDEX ( '%#%' , wf_name )+1 , LEN(wf_name)-(PATINDEX ( '%#%' , wf_name )) )
However I want to take the name also (everything left of the #). Unfortuantely I don't understand the functions I'm using above (having read the documentation I'm still confused). Could somebody please help me to understand the flow and how I can adjust this to get everything left of #?
Have a look at the following example
; WITH Table1 AS (
SELECT 'TADA#TEST' AS NameURL
)
SELECT *,
LEFT(NameURL,PATINDEX('%#%',NameURL) - 1) LeftText,
RIGHT(NameURL,PATINDEX('%#%',NameURL)- 1) RightText
FROM Table1
SQL Fiddle DEMO
Using functions
PATINDEX (Transact-SQL)
Returns the starting position of the first occurrence of a pattern in
a specified expression, or zeros if the pattern is not found, on all
valid text and character data types.
LEFT (Transact-SQL)
Returns the left part of a character string with the specified number
of characters.
RIGHT (Transact-SQL)
Returns the right part of a character string with the specified number
of characters.