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

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.

Related

Wildcard for string before # character

How would I use a wildcard to filter out any permutation of the following. There can be any number of zeros before the "#" character.
Example
0#test.com
000000#test.com
00000000000#test.com
Basically, I'm looking to wildcard any email address with only zeros before the # character.
Any help would be greatly appreciated
Thank you !
Smiddy
T-SQL:
To remove all values with only zeros before the #
SELECT
*
FROM (SELECT '000#test.com' AS[values] ) [table]
WHERE
LEN(REPLACE((LEFT([values],CHARINDEX('#',[Values])-1)),'0','')) <> 0
-- LEFT & CHARINDEX : get the result LEFT of the #
-- LEN & REPLACE : After all 0 are replaced by '' any email that contained only zeros will result in len = 0.
I would solve it with a regular expression, but the concrete answer depends on your database vendor and the available syntax.
PostgreSQL:
SELECT * FROM table WHERE email ~ '^0+#'
MySQL:
SELECT * FROM table WHERE email REGEXP '^0+#'
Synonyms are email RLIKE '^0+#' and REGEXP_LIKE(email, '^0+#').
Oracle:
SELECT * FROM table WHERE REGEXP_LIKE(email, '^0+#')

Select the first word on the left of a string in snowflake

I have a column (mycolumn) in my snowflake table (mytable) whose content has this pattern :
JohnDoe - Client Number One
MaryJane - Client Number Two
I would need to extract the first portion on the left of the string (JohnDoe,MaryJane - with no whitespace behind).
I tried to use the following approach, but I got stucked because I could only remove the first two block of words to the right, but not the - (dash) and the white spaces.
select substring(mycolumn,1,length(mycolumn)- CHARINDEX(' ', REVERSE(mycolumn))- CHARINDEX(' ', REVERSE(mycolumn))) from mytable
You can use regexp_substr():
select regexp_substr(mycolumn, '^[^ ]+')
from mytable;

Character position number in string

Try to get from the value - "some kind of tex #123fpe2"
all characters ​​after the # sign
Prepared the code below
but the problem is that not always the length of the value after the sign # 8 characters
select REVERSE(Left(Reverse(vValue), 8)),
(
select vValue from Runtime.dbo.Live where TagName = 'CurrentBaseRecipeName32000000io') as ValueForCheck,
from Runtime.dbo.Live where TagName = 'CurrentBaseRecipeName32000000io'
Getting error:
select REVERSE(Left(Reverse(vValue), POSITION('#' IN vValue))),
(
select vValue ...
select REVERSE(Left(Reverse(vValue), 8)),
(
select vValue from Runtime.dbo.Live where TagName = 'CurrentBaseRecipeName32000000io') as ValueForCheck,
from Runtime.dbo.Live where TagName = 'CurrentBaseRecipeName32000000io'
If you are sure that there is only 1 # in the string, or if there are more than 1 and you want the part of the string after the last occurrence of #, then you can use SUBSTRING_INDEX():
SELECT SUBSTRING_INDEX('some kind of tex #123fpe2', '#', -1);
will return:
123fpe2
I assume this is MySQL because of the use of POSITION in the question. Use INSTR to get the position of the '#' and then SUBSTRING to return everything after the index returned by INSTR + 1
SELECT SUBSTRING(vValue, INSTR(vValue, '#') + 1) FROM Runtime.dbo.Live
WHERE ...
Just in case, here is a version for SQL Server using RIGHT
SELECT RIGHT(vValue, LEN(vValue) - CHARINDEX('#', vValue)) FROM Runtime.dbo.Live
WHERE ...

Remove part of an email address

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;

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]