Remove part of an email address - sql

I have emails like sales#joebloggs.com.
I am looking to select out everything after the # and before the ..
Result should be joebloggs.

Simple regex pattern should do the job.
SELECT regexp_matches('sales#joebloggs.subdomain.com', '#([^.]+)\.');
SQL Fiddle
Edit: fixed to support subdomains. I assume you want to get the part before first dot.

Here is a solution using substring and position:
substring(col from (position('#' in col)+1)
for (
position('.' in substring(col from (position('#' in col)+1))) - 1
)
)

Solution using substring and instr
SELECT
substring('sales#joebloggs.com', instr('sales#joebloggs.com', '#') + 1, instr('sales#joebloggs.com', '.') - (instr('sales#joebloggs.com', '#') + 1))
FROM dual;

Related

DB2 - How to retrieve the last substring starting from the end

i'm trying to retrieve the last substring from a string, starting from the end.
Here it is my dataset:
Input:
BRAND_Arnette
BRAND_Persol
MODEL_CODE_DISPLAY_226781
Output:
Arnette
Persol
226781
What i 've managed to do is to retrieve what i need, but i'm not using an universal approach, because i'm considerging always the latest 10 chars, starting from the right:
SELECT
SUBSTR(RIGHT(rtrim(cast(attrval.IDENTIFIER as char(50))), 10), LOCATE('_',RIGHT(rtrim(cast(attrval.IDENTIFIER as char(50))), 10))+1)
FROM ...
How can this select be edited so it can be always valid? Thanks
Try the following expression:
SUBSTR (identifier, LOCATE_IN_STRING (identifier, '_', -1) + 1)
dbfiddle example.
I would suggest regexp_substr():
select regexp_substr(identifier, '[^_]+$')
Here is a db<>fiddle.

TRIM forward slash in T-SQL

I have the following code...
TRIM(LEADING '/' FROM ci.Long_description_1) as 'Description',
I am getting the error message
Incorrect syntax near '/'
Whats the best way of writing this?
Thanks
If you are using SQL Server 2017+, you may use TRIM() with a small trick (by default TRIM() removes the specified characters from the start and the end of the string):
SELECT LEFT(
TRIM('/' FROM Long_description_1 + '?'),
LEN(TRIM('/' FROM Long_description_1 + '?')) - 1
) AS Description
FROM (VALUES
(NULL),
('abcd'),
('1234/'),
('////abcd'),
('/folder/subfolder/x.yz')
) ci (Long_description_1)
Result:
Description
----------------------
abcd
1234/
abcd
folder/subfolder/x.yz
I think you want to remove '/' if it is the first character of Long_description_1 column value. TRIM function in SQL Server will not work the way you are expecting.
For this you can write your query like following.
SELECT CASE
WHEN CHARINDEX('/', ci.Long_description_1) = 1
THEN RIGHT(ci.Long_description_1, LEN(ci.Long_description_1) - 1)
ELSE ci.Long_description_1
END AS [Description]
FROM YouTable

How to get the domain name without '.com'?

SELECT substr(Emails, instr(Emails, '#')+1)
FROM EmployeeEmails;
Returns
gmail.com
Do I need to concat to get:
gmail
Check your relevant database query :
Query(for MySQL)
select (SUBSTRING_INDEX(SUBSTR(Emails, INSTR(Emails, '#') + 1),'.',1)) from EmployeeEmails;
Query(For Sql Server):
select SUBSTRING(Emails,(CHARINDEX('#',Emails)+1),1) from EmployeeEmails;
Query(For Oracle)
select substr(Emails,instr(Emails,'#',1)+1) as domain
from EmployeeEmails;
You can use REGEXP_REPLACE to extract the domain name:
select regexp_replace(emails, '^[^#]+#([^.]+)\..+$', '\1') from employeeemails;
This works for any email of the pattern abcd#efgh.ijkl .
The pattern:
^ start of the sting
[^#]+ 1 to n characters other than #
# the at sign #
( remember the following string
[^.]+ 1 to n characters other than the dot .
) end of the string to remember
\. a dot .
.+ 1 to n characters
$ end of the string
\1 the remembered string
And here is the old-fashioned way without REGEXP_REPLACE:
select substr(emails,
instr(emails, '#') + 1,
instr(emails, '.', instr(emails, '#') + 1) - instr(emails, '#') - 1
)
from employeeemails;
try this : -
declare #email sysname = 'testCode#gmail.com'
SELECT substring(#email, charindex('#',#email,0)+1 , (charindex('.',#email,0)-1 - charindex('#',#email,0)))
You can use LIKE with wild cards see here
underscore character ( _ ) for any single character.
percent sign character (%) for a string of zero or more characters.
SELECT email FROM emails
WHERE email NOT LIKE '%_#__%.__%'
This will ignore the following cases (simple version for valid emails):
emails that have at least one character before the #
emails that have at least two characters between # and .
emails that have at least two characters between . and the end.

How to replace all the dots before # in an email with empty string in Oracle SQL?

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]

Extract text before third - "Dash" in SQL

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...