Sample Mail ID: maniyuva21_etc#xyz.com
Here I should remove chars from _etc. But in same way if _etc is not available in some mail ID then should remove from #.
So my result will be maniyuva21
I need this to be done in Oracle query
I would use REGEX_REPLACE.
Example from SQLPLUS:
SQL> SELECT REGEXP_REPLACE('maniyuva21_etc#xyz.com', '(_etc)?#.*$', '') FROM dual;
REGEXP_REP
----------
maniyuva21
SQL> SELECT REGEXP_REPLACE('maniyuva21#xyz.com', '(_etc)?#.*$', '') FROM dual;
REGEXP_REP
----------
maniyuva21
NOTE: Oracle engine must be >= 10g
Use a CASE expression to check whether the email string contains _etc or not. Then use a combination of SUBSTR and INSTR to extract the required part.
Query
select email_column,
case when email_column like '%_etc#%'
then substr(email_column, 1, instr(email_column, '_etc#', 1) - 1)
else substr(email_column, 1, instr(email_column, '#', 1) - 1) end as new_email_column
from your_emails_table;
Related
SQL....On my table I have attribute table with “Pat0700-1700” on my report I want to drop the Pat and only display 0700-1700. How would I accomplish this on SQL. I have search and tried the substring with neg results.
On these following RDBMS:
Oracle
MySQL
DB2
StandardSQL
you can try with the function SUBSTR():
SELECT SUBSTR(<column>, 4) AS substr_string
FROM <table>
OUTPUT:
substr_string
-------------
0700-1700
The standard SQL method would be replace():
select replace(col, 'Pat', '')
Given that the rest of the string has a fixed format -- 9 characters -- you might also find that one of these is appropriate (and more general):
select right(col, 9)
select substr(col, 4, 9) -- or perhaps substring()
i want to get words after specific character in pl/sql
for example :
text = '2 - 99 - 7051B'
I want to see 7051B which means after second '-' to the last character.
function try ( text in varchar2 )
is begin
v_textout varchar2(100) := '';
--some process
return v_textout;
end;
No need for PL/SQL to extract the desired part. Two alternative methods use REGEXP_REPLACE() and REGEXP_SUBSTR() regular expression functions respectively
WITH t(text) AS
(
SELECT '2 - 99 - 7051B' FROM dual
)
SELECT REGEXP_REPLACE(text,'(.*- )(\S+)','\2') AS first_method,
REGEXP_SUBSTR(text,'[^- ]+$') AS second_method
FROM t;
FIRST_METHOD SECOND_METHOD
------------ -------------
7051B 7051B
Demo
where the spaces after dash characters are left deliberately according to the sample, and plus(+) stands for one or more occurences for the match .
I found the solution :
select substr(text , instr(text , '-', 1, 2) + 1, length(text))
from dual;
I need to extract every thing before last two periods
eg.
Input: AA.BBB.12.11.cc
Output: AA.BBB.12
Following is the sample query I am using but that returns only the characters before first period, but that is not I needed.
SELECT REGEXP_SUBSTR(t.column,'[^.]+',1,1)
AS output
FROM MY_Table t where t.column is not null and rownum=1
I would use REGEXP_REPLACE here:
SELECT REGEXP_REPLACE(t.column, '\.[^.]+\.[^.]+$', '')
FROM MY_table
WHERE t.column IS NOT NULL AND rownum = 1;
The regex pattern \.[^.]+\.[^.]+$ will match starting with the second to last dot, all content until the end (including also the last dot).
You can simply use INSTR and SUBSTR as following:
SQL> SELECT
2 SUBSTR('AA.BBB.12.11.ccCC', 1, INSTR('AA.BBB.12.11.ccCC', '.', -2, 2) - 1) AS RESULT
3 FROM DUAL;
RESULT
---------
AA.BBB.12
SQL>
-- Update --
For the question asked in the comment, use the following query:
SQL> SELECT
2 SUBSTR('AA.BB.CC.DD', 1, INSTR('AA.BB.CC.DD', '.', 1, 3) - 1) AS RESULT
3 FROM DUAL;
RESULT
--------
AA.BB.CC
SQL>
Cheers!!
I want to replace all the dots before # in an email with empty string in oracle query
like:
anurag.mart#hotmail.com >> anuragmart#hotmail.com
Instr - To identify the position(#)
Substr - To extract data between start(1) and end(#) position
Replace - To replace . with ''
|| - To concatenate two strings
Try this
SELECT Replace(Substr('anurag.mart#hotmail.com', 1,
Instr('anurag.mart#hotmail.com', '#', 1)), '.', '')
|| Substr('anurag.mart#hotmail.com', Instr('anurag.mart#hotmail.com','#')+1)
FROM dual
Result:
anuragmart#hotmail.com
SqlFiddle Demo
The easiest way is to use REGEXP_REPLACE to identify the pattern and replace it with required pattern.
regexp_replace('anurag.mart#hotmail.com', '(\w+)\.(\w+)(#+)', '\1\2\3')
For example,
SQL> SELECT 'anurag.mart#hotmail.com' email_id,
2 regexp_replace('anurag.mart#hotmail.com', '(\w+)\.(\w+)(#+)', '\1\2\3') new_email_id
3 FROM dual;
EMAIL_ID NEW_EMAIL_ID
----------------------- ----------------------
anurag.mart#hotmail.com anuragmart#hotmail.com
I came on this page while looking for solutions for SQL servers, I converted the above for SQL server for my project, Here is SQL if anybody else needs it.
SELECT
CONCAT(
REPLACE(
SUBSTRING(EmailAddress, 1, CHARINDEX('#', EmailAddress)-1),
'.',
''
),
SUBSTRING(EmailAddress, CHARINDEX('#', EmailAddress), LEN(EmailAddress))
)
FROM [Applicant]
Can you please help to get this code for SQL?
I have column name INFO_01 which contain info like:
D10-52247-479-245 HALL SO
and I would like to extract only
D10-52247-479
I want the part of the text before the third "-" dash.
You'll need to get the position of the third dash (using instr) and then use substr to get the necessary part of the string.
with temp as (
select 'D10-52247-479-245 HALL SO' test_string from dual)
select test_string,
instr(test_string,1,3) third_dash,
substr(test_string,1,instr(test_string,1,3)-1) result
from temp
);
Here is a simple statement that should work:
SELECT SUBSTR(column, 1, INSTR(column,'-',1,3) ) FROM table;
Using a combination of SUBSTR and INSTR will return what you want:
SELECT SUBSTR('D10-52247-479-245', 0, INSTR('D10-52247-479-245', '-', -1, 1)-1) AS output
FROM DUAL
Result:
output
-------------
D10-52247-479
Use:
SELECT SUBSTR(t.column, 0, INSTR(t.column, '-', -1, 1)-1) AS output
FROM YOUR_TABLE t
Reference:
SUBSTR
INSTR
Addendum
If using Oracle10g+, you can use regex via REGEXP_SUBSTR.
I'm assuming MySQL, let me know if I'm wrong here. But using SUBSTRING_INDEX you could do the following:
SELECT SUBSTRING_INDEX(column, '-', 3)
EDIT
Appears to be oracle. Looks like we may have to resort to REGEXP_SUBSTR
SELECT REGEXP_SUBSTR(column, '^((?.*\-){2}[^\-]*)')
Can't test, so not sure what kind of result that will have...