Select everything to the right of a specific character - sql

Given this data:
Home: (708) 296-2112
I want everything to the right of the : character.
This is what I have so far, but I'm getting no results:
right(phone1, locate(':', phone1 + ':')-1) phone
If I use left instead of right, I get just "HOME" - just for testing purposes. I know I'm close, but I'm missing something.

You can use SUBSTRING (might be SUBSTR dependent on your version) instead:
SELECT SUBSTRING(phone1, LOCATE(':', phone1) + 1, LENGTH(phone1))
FROM yourtable

Here's a way to do it without hard-coding in Home:, so you can also use Office: or Mobile: or Fax:, or any other word followed by a colon.
This uses ADS's scripting ability to use a variable and the built-in System.iota single row table (similar to Oracle's dual). You can just use the last line, replacing test with the name of your column and system.iota with the name of your table.
declare test string;
set test = 'Home: (708) 296-2112';
select substring(test, position(':' in test) + 1, length(test)) from system.iota;

You were on the right track, but your algebra is off. You want to take the full length of the string offset by the position of the colon, minus one:
right(phone1, length(phone1) - locate(':', phone1) - 1)

You can use RIGHT function as follows:
RIGHT(phone1, LEN(phone1)-CHARINDEX(':', phone1))

Related

replace all occurrences of a sub string between 2 charcters using sql

Input string: ["1189-13627273","89-13706681","118-13708388"]
Expected Output: ["14013627273","14013706681","14013708388"]
What I am trying to achieve is to replace any numbers till the '-' for each item with hard coded text like '140'
SELECT replace(value_to_replace, '-', '140')
FROM (
VALUES ('1189-13627273-77'), ('89-13706681'), ('118-13708388')
) t(value_to_replace);
check this
I found the right way to achieve that using the below regular expression.
SELECT REGEXP_REPLACE (string_to_change, '\\"[0-9]+\\-', '140')
You don't need a regexp for this, it's as easy as concatenation of 140 and the substring from - (or the second part when you split by -)
select '140'||substring('89-13706681' from position('-' in '89-13706681')+1 for 1000)
select '140'||split_part('89-13706681','-',2)
also, it's important to consider if you might have instances that don't contain - and what would be the output in this case
Use regexp_replace(text,text,text) function to do so giving the pattern to match and replacement string.
First argument is the value to be replaced, second is the POSIX regular expression and third is a replacement text.
Example
SELECT regexp_replace('1189-13627273', '.*-', '140');
Output: 14013627273
Sample data set query
SELECT regexp_replace(value_to_replace, '.*-', '140')
FROM (
VALUES ('1189-13627273'), ('89-13706681'), ('118-13708388')
) t(value_to_replace);
Caution! Pattern .*- will replace every character until it finds last occurence of - with text 140.

split string with character

using SQL 2008; I have the following string:
EMCo: 1 WorkOrder: 12770 WOItem: 10
I am trying to get the WorkOrder #.
When the string did not have the WOItem on end of it, I was able to use the following statement to get WorkOrder #.
[WorkOrder] = LTRIM(RTRIM(RIGHT(HQMA.KeyString,CHARINDEX(':',REVERSE(HQMA.KeyString))-1)))
This statement moves and may have double digits for the Co#, and it does not always have WOItem #. Was hoping to find something that would split after the ":" and just take 2nd group.
Any suggestions?
The patindex suggestion above will work beautifully, now if you still want to use your current statement, substring will pull the same values, and replace will take out WOItem. Not very elegant, it works whether you have WOItem or not:
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
select substring(LTRIM(RTRIM(RIGHT(REPLACE(HQMA.KeyString,'WOItem:',''),
CHARINDEX(':',REVERSE(REPLACE(HQMA.KeyString,'WOItem:','')))-1))),0,7)
How about using patindex()? Assuming the work order always has five characters:
select substring(HQMA.KeyString,
patindex('%WorkOrder: %', HQMA.KeyString) + 11,
5) as WorkOrder

Comparing fields when a field has data in between 2 characters that match the field being compared

I have code that looks like this:
left outer join
gme_batch_header bh
on
substr(ln.lot_number,instr(ln.lot_number,'(') + 1,
instr(ln.lot_number,')') - instr(ln.lot_number,'(') - 1)
=
bh.batch_no
It works fine, but I have come across a few lot numbers that have two sections of strings that are between parenthesis. How would I compare what is between the second set of parenthesis? Here is an example of the data in the lot number field:
E142059-307-SCRAP-(74055)
This one works with the code,
58LF-3-B-2-2-2 (SCRAP)-(61448)
This one tries comparing SCRAP with the batch no, which isn't correct. It needs to be the 61448.
The result is always the last item in parenthesis.
After more research, I actually got it to work with this code:
substr(ln.lot_number,instr(ln.lot_number,'(',-1) + 1, instr(ln.lot_number,')',-1) - instr(ln.lot_number,'(',-1) - 1)
Assuming SQL2005+, and it is always the last occurrence you want, then I would suggest finding the last instance of a ( in your query and substring to there. To get the last instance you could use something like:
REVERSE(SUBSTRING(REVERSE(lot_number),0,CHARINDEX('(',REVERSE(lot_number))))
If your version of Oracle supports regular expressions try this:
substr(regexp_substr(ln.lot_number,'[0-9]+\)$'),1,length(regexp_substr(ln.lot_number,'[0-9]+\)$'))-1)
Explanation:
regexp_substr(scrap_row,'[0-9]+\)$' ==> find me just numbers in the string that ends in ). This returns the numbers but it includes the closing parenthesis.
To remove the closing parenthsis, just send it through substring and extract first number through the length of the number stopping at 1 character from the end of the string.
Query for analysis:
with scrap
as (select '58LF-3-B-2-2-2 (SCRAP)-(61448)' as scrap_row from dual)
select scrap_row,
regexp_substr(scrap_row,'[0-9]+\)$') as regex_substring,
length(regexp_substr(scrap_row,'[0-9]+\)$')) as length_regex_substring,
substr(regexp_substr(scrap_row,'[0-9]+\)$'),1,length(regexp_substr(scrap_row,'[0-9]+\)$'))-1) as regex_sans_parenthesis
from scrap
If you have 11g, this will do it pretty simply by using the subgroup argument of regexp_substr() and constructing the regex appropriately:
SQL> with tbl(data) as
(
select 'E142059-307-SCRAP-(74055)' from dual
union
select '58LF-3-B-2-2-2 (SCRAP)-(61448)' from dual
)
select data from tbl
where regexp_substr(data, '\((\d+)\)$', 1, 1, NULL, 1)
= '61448';
DATA
------------------------------
58LF-3-B-2-2-2 (SCRAP)-(61448)
The regular expression can be read as:
\( - Search for a literal left paren
( - Start a remembered subgroup
\d+ - followed by 1 more more digits
) - End remembered subgroup
\) - followed by a literal right paren
$ - at the end of the line.
The regexp_substr function arguments are:
Source - the source string
Pattern - The regex pattern to look for
position - Position in the string to start looking for the pattern
occurrence - If the pattern occurs multiple times, which occurrence you want
match_params - See the docs, not used here
subexpression - which subexpression to use (the remembered group)
So in English, look for a series of 1 or more digits surrounded by parens, where it occurs at the end of the line and save the digit part only to use to compare. IMHO a lot easier to follow/maintain than nested instr(), substr().
For re-useability, make a function called get_last_number_in_parens() that contains this code and uses an argument of the string to search. This way that logic is encapsulated and can be re-used by folks that may not be so comfortable with regular expressions, but can benefit from the power! One place to maintain code too. Then call like this:
select data from tbl
where get_last_number_in_parens(data) = '61448';
How easy is that?!
Hello you can check with this code. It works whaever the condition may be
SELECT SUBSTR('58LF-3-B-2-2-2-(61448)',instr('58LF-3-B-2-2-2-(61448)','(',-1)+1,LENGTH('58LF-3-B-2-2-2-(61448)')-instr('58LF-3-B-2-2-2-(61448)','(',-1)-1)
FROM dual;
SELECT SUBSTR('58LF-3-B-2-2-2 (SCRAP)-(61448)',instr('58LF-3-B-2-2-2 (SCRAP)-(61448)','(',-1)+1,LENGTH('58LF-3-B-2-2-2 (SCRAP)-(61448)')-instr('58LF-3-B-2-2-2 (SCRAP)-(61448)','(',-1)-1)
FROM dual;
Output
==================================
61448
==================================

SQL Get Text Between \

I'm having an issue getting the correct text between characters. I'm currently trying to use SUBSTRING and CHARINDEX. The issue is that the data contains the same identifier. I would like the text that falls between the first '\' and the second '\', removing all the other text. None of the text fields have a fixed number of characters.
Thank you for your help its greatly appreciated.
Example
1. Location\Georgia\Atlanta
2. Country\USA\States\Minnesota
Final Result
1. Georgia
2. USA
My Current Attempt
SUBSTRING(Source,CHARINDEX('\',Source),CHARINDEX('\',Source) - CHARINDEX('\',Source))
I agree with Joel's comment. This does look like a violation of first normal form but to find the second instance of \ you can pass the location of the first one in as the third argument to CHARINDEX
WITH T(Source) AS
(
SELECT 'Location\Georgia\Atlanta' UNION ALL
SELECT 'Country\USA\States\Minnesota'
)
SELECT *,
SUBSTRING(Source, F,CHARINDEX('\',Source, F) - F)
FROM T
CROSS APPLY (SELECT 1 + CHARINDEX('\',Source)) CA(F)
WHERE Source LIKE '%\%\%'
Martin's answer is slick, this method works, but mostly just illustrates how slick his is:
WITH T(Source) AS
(
SELECT 'Location\Georgia\Atlanta' UNION
SELECT 'Country\USA\States\Minnesota'
)
SELECT *, SUBSTRING(Source,CHARINDEX('\',Source)+1,CHARINDEX('\',Source,CHARINDEX('\',Source)+1)-CHARINDEX('\',Source)-1)
FROM T

How do I select everything after a certain position using REGEXP in Oracle

I am using Oracle 11G and I have the following string: I - Am in- Need Help- Please and want to parse the string using the - character then select everything after the second object. I have been playing around with REGEXP_REPLACE and SUBSTR and I can select the second object but can't seem to select everything after the second object. I know I need to add a * somewhere I think but I can't seem to get the syntax correct. Any help would be greatly appreciated.
SELECT REGEXP_SUBSTR ('I - Am in- Need of Help- Please', '[^-]+' , 1, 2)
FROM DUAL;
The above query returns the second object 'Am in' but I want this returned 'Need of Help- Please'. It is also important to note that I don't want the - character directly following the second object in the result either but I want to start with the third object.
Since [^-] will only match non-hyphen characters, your match group will start after the first hyphen and then stop before the next one. To capture everything following the first hyphen, try:
SELECT REGEXP_REPLACE(REGEXP_SUBSTR ('I - Am in- Need of Help- Please', '-.+' , 1), '-', '', 1, 1) FROM DUAL;