SQL Server Concatenate '%[^a-z0-9 .]%' to a string and compare parts of strings? [closed]

qyzbxkaa  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(86)

Closed. This question needs to be more focused . It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post .

Closed 10 hours ago.
Improve this question

How do I evaluate if any string (the first component of an email address [name] field) has an equivalent email followed by a non alphanumeric character. How do I say concat[name] plus all possible non alpha numeric characters and see if exists or in or like the email in the master table.

My data (local table).
| Email | email_id | name | domain | ext |
| ------------ | ------------ | ------------ | ------------ | ------------ |
| email protected(https://stackoverflow.com/cdn-cgi/l/email-protection) | 1234 | HelloWorld | gmail | com |
| email protected(https://stackoverflow.com/cdn-cgi/l/email-protection) | 678 | jon.smithII | nyu | edu |

master table

| Email | id  | 
|  [email protected]  | 1234  | 
|  [email protected]  | 1235  | 
|  [email protected]  | 1336  | 
|  [email protected]  | 1237  |
t9aqgxwy

t9aqgxwy1#

It's really helpful if you provide the DDL/DML, in an easy to reproduce format.

DECLARE @Emails TABLE (Email NVARCHAR(100), EmailID INT);
INSERT INTO @Emails (Email, EmailID) VALUES
('[email protected]', 1234),('[email protected]', 1235), ('[email protected]', 1236), ('[email protected]', 6780);

SELECT e.Email, e.EmailID, CASE WHEN PATINDEX('%[A-Z|a-z|0-9]@%', e.Email) > 0 THEN 1 ELSE 0 END AS AlpaNumericBeforeAt, LEFT(e.Email,CHARINDEX('@',e.Email)-1) AS UserName,
       SUBSTRING(e.Email,CHARINDEX('@',e.Email)+1,LEN(e.Email)-(CHARINDEX('@',e.Email)+CHARINDEX('.',REVERSE(e.Email)))) AS Domain, 
       REVERSE(LEFT(REVERSE(e.Email),CHARINDEX('.',REVERSE(e.Email))-1)) AS Extenstion
  FROM @Emails e;
EmailEmailIDAlpaNumericBeforeAtUserNameDomainExtenstion
email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)12341HelloWorldgmail.butnotcom
email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)12351HelloWorldgmailcom
email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)12360HelloWorl+gmailcom
email protected(https://stackoverflow.com/cdn-cgi/l/email-protection)67801jon.smithIInyuedu

Here we're using the string manipulation functions LEFT , SUBSTRING , PATINDEX , REVERSE and CHARINDEX to slice and dice the emails up. https://learn.microsoft.com/en-us/sql/t-sql/functions/string-functions-transact-sql?view=sql-server-ver16 .

I've assumed A-Z a-z and 0-9 covers the alpha-numeric range you wanted.

This is probably a job for your favorite ETL tool, but could work in a pinch. You may want to consider storing the results rather than regenerating them all the time.

相关问题