Get text after second occurrence of a specific character - sql

I have a column with data like this:
firstNameLetter_surname_numericCode
For example:
m_johnson_1234
I need to extract the numeric code. I've tried with SUBSTRING function but I just get:
surname_numericCode
This is the code I've used:
SET number = substring(t2.code, charindex('_', t2.code, 2) + 1, len(t2.code))
How can I get just the numeric code?

Heres a one-liner for you
select right('m_johnson_1234', charindex('_', reverse('m_johnson_1234') + '_') - 1)

Call the CHARINDEX function twice:
SELECT SUBSTRING(
code,
NULLIF(CHARINDEX('_', code, NULLIF(CHARINDEX('_', code), 0) + 1), 0) + 1,
LEN(code)
)
FROM (VALUES
('a_b_c'),
('a_b')
) x(code)

One method is to look for the first _ in the reversed string:
select col,
stuff(col, 1, len(col) - charindex('_', reverse(col)) + 1, '') as numericCode
from (values ('firstNameLetter_surname_numericCode')) v(col);
If the numeric code is really a number -- and no other numbers start the preceding values -- then you can use patindex():
select col,
stuff(col, 1, patindex('%[_][0-9]%', col), '') as numericCode
from (values ('firstNameLetter_surname_0000')) v(col);

The SUBSTR and INSTR functions can be combined to get you the numeric code.
SELECT SUBSTR('m_johnson_1234', INSTR('m_johnson_1234', '_', 1, 2)+1) FROM TABLE;
For the start_pos argument use INSTR to start at the beginning of the string, and find the index of the second instance of the '_' character. Then use that to start one position after and read to the end of the string.
If you need the result to be numeric instead of still a string, then wrap the SUBSTR() in a TO_NUMBER() function.

Late answer, and just because I didn't see PARSENAME() mentioned.
Example
Select parsename(replace(t2.code,'_','.'),1)
From YourTable

Related

How can I use SQL Substring to extract characters from this filename?

I'm attempting to use the SUBSTRING function to extract a name out of a filename.
An example filename would be: "73186_RHIMagnesita_PHI_StopLoss_TruncSSN_NonRedact_Inc_to_Apr2022_Paid_to_Apr2022_EDIT"
I'm attempting to extract the "RHIMagnesita" from this filename.
The substring I used was:
SUBSTRING(DFH.FileName, CHARINDEX('_', DFH.FileName) + 1, CHARINDEX('_PHI', DFH.FileName) - 1)
The results it gave were: "RHIMagnesita_PHI_S"
How do I extract only "RHIMagnesita" using the Substring function?
The third parameter in SUBSTRING is length not position, so you would need to substract the length of the beginning string.
SUBSTRING(DFH.FileName, CHARINDEX('_', DFH.FileName) + 1, CHARINDEX('_PHI', DFH.FileName) - CHARINDEX('_', DFH.FileName))
You might need to add or substract 1, but that's the idea.
You were close. You need to use CHARINDEX to also find the position of the second underscore.
SELECT SUBSTRING(FileName,
CHARINDEX('_', FileName) + 1,
CHARINDEX('_', FileName, CHARINDEX('_', FileName) + 1) -
CHARINDEX('_', FileName) - 1) AS FilePart
FROM yourTable;
Here's a way using STRING_SPLIT and FETCH, rather than SUBSTRING We split the string and only return the second row
SELECT
value
FROM STRING_SPLIT('73186_RHIMagnesita_PHI_StopLoss_TruncSSN_NonRedact_Inc_to_Apr2022_Paid_to_Apr2022_EDIT','_')
ORDER BY (SELECT NULL)
OFFSET 1 ROWS
FETCH NEXT 1 ROWS ONLY;
Note: On Azure Sql Server STRING_SPLIT has an ordinal parameter, so you could write this
SELECT
value
FROM
STRING_SPLIT('73186_RHIMagnesita_PHI_StopLoss_TruncSSN_NonRedact_Inc_to_Apr2022_Paid_to_Apr2022_EDIT','_', 1)
WHERE ordinal = 2

is there a way to use a mid-style function in sql like you can for excel?

I have the following string of characters:
594074_Ally_Financial_TokioMarine_MD_SLDET_20210101_20211130_20211208
I am attempting to extract everything after the first '_' but before the '_TokioMarine', so the final string will look like:
Ally_Financial
Is this possible to do with SQL? I attempted but it was pulling the incorrect characters. I cant get the ones in between the values specified.
SELECT
#CurPolicyHolder = Right( DFH.FileName, CHARINDEX('_', DFH.FileName) - 1)
To extract everything between the first _ character and the _TokyoMarine string, you can use:
SELECT
#CurPolicyHolder = SUBSTRING(DFH.FileName, CHARINDEX('_', DFH.FileName) + 1,
CHARINDEX('_TokioMarine', DFH.FileName) - CHARINDEX('_', DFH.FileName) - 1)
SUBSTRING (Transact-SQL)
CHARINDEX (Transact-SQL)

Oracle - How to extract delimited string

I have a sample String as below,
A|SDFR|RESTA|PRET|PRUQA
B|121|BBCTRI|9ALFA|DEV|5AS
I want to extract the part that is coming after send delimiter,
Expected,
RESTA|PRET|PRUQA
BBCTRI|9ALFA|DEV|5AS
What i got is just extracting single characters regexp_substr
Assuming you mean after the second delimiter, you don't need to use regular expressions for this; you can use the basic ]substr()](http://docs.oracle.com/database/121/SQLRF/functions196.htm) function, getting the starting position with instr():
substr(<your_string>, instr(<your_string>, '|', 1, 2) + 1)
The third argument to instr() says you want the second occurrence; the second argument says you're starting from position 1. That then points to the second delimiter, and you want to start at the next character after that delimiter, so have to add 1.
Demo:
with t (str) as (
select 'A|SDFR|RESTA|PRET|PRUQA' from dual
union all select 'B|121|BBCTRI|9ALFA|DEV|5AS' from dual
)
select substr(str, instr(str, '|', 1, 2) + 1)
from t;
SUBSTR(STR,INSTR(STR,'|',1
--------------------------
RESTA|PRET|PRUQA
BBCTRI|9ALFA|DEV|5AS
try this:
substr(string, instr(string, '|', 1, 2)+1)
Maybe like this;
with a as (select 'B|121|BBCTRI|9ALFA|DEV|5AS' test from dual)
select substr(test,instr(test,'|',3)+1) from a

oracle 12c - select string after last occurrence of a character

I have below string:
ThisSentence.ShouldBe.SplitAfterLastPeriod.Sentence
So I want to select Sentence since it is the string after the last period. How can I do this?
Just for completeness' sake, here's a solution using regular expressions (not very complicated IMHO :-) ):
select regexp_substr(
'ThisSentence.ShouldBe.SplitAfterLastPeriod.Sentence',
'[^.]+$')
from dual
The regex
uses a negated character class to match anything except for a dot [^.]
adds a quantifier + to match one or more of these
uses an anchor $ to restrict matches to the end of the string
You can probably do this with complicated regular expressions. I like the following method:
select substr(str, - instr(reverse(str), '.') + 1)
Nothing like testing to see that this doesn't work when the string is at the end. Something about - 0 = 0. Here is an improvement:
select (case when str like '%.' then ''
else substr(str, - instr(reverse(str), ';') + 1)
end)
EDIT:
Your example works, both when I run it on my local Oracle and in SQL Fiddle.
I am running this code:
select (case when str like '%.' then ''
else substr(str, - instr(reverse(str), '.') + 1)
end)
from (select 'ThisSentence.ShouldBe.SplitAfterLastPeriod.Sentence' as str from dual) t
And yet another way.
Not sure from a performance standpoint which would be best...
The difference here is that we use -1 to count backwards to find the last . when doing the instr.
With CTE as
(Select 'ThisSentence.ShouldBe.SplitAfterLastPeriod.Sentence' str, length('ThisSentence.ShouldBe.SplitAfterLastPeriod.Sentence') len from dual)
Select substr(str,instr(str,'.',-1)+1,len-instr(str,'.',-1)+1) from cte;
select
substr(
'ThisSentence.ShouldBe.SplitAfterLastPeriod.Sentence',
INSTR(
'ThisSentence.ShouldBe.SplitAfterLastPeriod.Sentence',
'.',
-1
)+1
)
from dual;
The INSTR function accepts a third parameter, the occurrence. It defaults to 1 (the first occurrence), but also accepts negative numbers (meaning counting from the last occurrence backwards).
select substr(str, instr(str, '.', -1) + 1)
from (
select 'ThisSentence.ShouldBe.SplitAfterLastPeriod.Sentence'
as str
from dual);
Sentence
how many dots in a string?
select length(str) - length(replace(str, '.', '') number_of_dots from ...
get substring after last dot:
select substr(str, instr(str, '.', 1, number_of_dots)+1) from ...

extract characters using delimiters

I am having string called '200_CFL_2010'. I wish to get the characters between _'s. I want the output as 'CFL'. I have to achieve it through SQL.
I think this may be dependent on your Database (MySQL, Oracle, whatever..)
Do a search for "string functions" and your database name. For e.g MySQL, you can find it here: http://dev.mysql.com/doc/refman/5.1/en/string-functions.html.
The function INSTR and SUBSTR are the ones you are looking for. Use then like
SELECT SUBSTR(field, INSTR(field, "_", 1), INSTR(field, "_", 2) - INSTR(field, "_", 1)) FROM ...
Note: INSTR does only have two parameters in MySQL... youd need to cascade SUBSTRs a little there.
select substring(col, 0, LEN(col) - charindex('_', col)) as col
from
(
select substring(col, charindex('_', col) + 1, LEN(col)) as col
from
(
select '200_CFL_2010' as col
) as subq
) as subq