简体   繁体   English

仅选择SQL中的字符

[英]Select only characters in SQL

I have strings in a database like this: 我在这样的数据库中有字符串:

firstname.lastname@email.com firstname.lastname@email.com

And I only need the characters that appear after the @ symbol and before (.) symbol ie (email) from the above example 而且我只需要出现在@符号之后(。)符号之前的字符,即上述示例中的(email)

I am trying to find a simple way to do this in SQL. 我正在尝试找到一种在SQL中执行此操作的简单方法。

Do this: 做这个:

use [your_db_name];
go

create table dbo.test
(
    string varchar(max) null
)
insert into dbo.test values ('firstname.lastname@email.com')

select 
    string,
    substring(
        string, 
        charindex('@', string, 0) + 1, 
        charindex('.', string,  charindex('@', string, 0)) - charindex('@', string, 0) - 1
    ) as you_need
from dbo.test

String manipulations are such a pain in SQL Server. 字符串操作在SQL Server中是如此痛苦。 Here is one method: 这是一种方法:

select t.*,
       left(en.emailname, charindex('.', en.emailname + '.') - 1)
from t outer apply
     (select stuff(email, 1, charindex('@', email + '@'), '') as emailname) en;

That that in the charindex() calls, the character being searched for is placed at the end of the string. charindex()调用中,要搜索的字符位于字符串的末尾。 This allows the code to work even for malformed emails -- it returns an empty string when the email is not of the form '%@%.%' . 这使代码甚至可以用于格式错误的电子邮件-当电子邮件的格式不是'%@%.%'时,它将返回一个空字符串。

DECLARE @str varchar(50) = 'firstname.lastname@email.com';
SELECT LEFT(
         RIGHT(@str, LEN(@str) - CHARINDEX('@', @str))
         ,CHARINDEX('.', RIGHT(@str, LEN(@str) - CHARINDEX('@', @str))
       ) - 1) AS OUTPUT

Above query gives only domain-name from Email. 上面的查询仅提供来自电子邮件的域名。 The query can be applied for column in a table 该查询可以应用于表中的列

DECLARE @col char(200) set @col = 'firstname.lastname@email.com' 宣告@col char(200)set @col ='firstname.lastname@email.com'

SELECT SUBSTRING(@col, LEN(LEFT(@col, CHARINDEX ('@', @col))) + 1, LEN(@col) - LEN(LEFT(@col, CHARINDEX ('@', @col))) - LEN(RIGHT(@col, LEN(@col) - CHARINDEX ('.', @col))) - 4); SELECT SUBSTRING(@col,LEN(LEFT(@col,CHARINDEX('@',@col)))+ 1,LEN(@col)-LEN(LEFT(@col,CHARINDEX('@',@col)) )-LEN(RIGHT(@col,LEN(@col)-CHARINDEX('。',@col)))-4);;

Try This:- 尝试这个:-

DECLARE @Text varchar(100)
SET @Text = 'firstname.lastname@email.com'
SELECT SUBSTRING(STUFF(@Text, 1, CHARINDEX('@',@Text), ''), 0, 
CHARINDEX('.', STUFF(@Text, 1, CHARINDEX('@',@Text), '')))

Result:- 结果:-

email

DECLARE @myStr varchar(100) = 'firstname.lastname@email.com'

SELECT 
SUBSTRING(SUBSTRING(@myStr,CHARINDEX('@',@myStr)+1,LEN(@myStr)-CHARINDEX('@',@myStr)+1),0,CHARINDEX('.',SUBSTRING(@myStr,CHARINDEX('@',@myStr)+1,LEN(@myStr)-CHARINDEX('@',@myStr)+1)))

That can be useful but I really recommend you to build user defined function in C#/Visaul basic they could be much more faster that this. 这可能很有用,但我真的建议您在C#/ Visaul basic中构建用户定义的函数,因为这样做可能会更快。

Using charindex, len and reverse to search for the positions of the @ and the last dot. 使用charindex,len和reverse来搜索@和最后一个点的位置。
And substring to get the name based on those positions: 子字符串根据这些位置获取名称:

create table test (id int identity(1,1), email varchar(60));

insert into test (email) values 
('jane.doe@email.com'),
('not an email'),
('@invalid.email.xxx'),
('john.doe@longer.domainname.net');

select *, 
 (case 
  when email like '[a-z]%@%.%' 
  then substring(email, 
          charindex('@',email)+1,
          len(email) - charindex('@',email) - charindex('.',reverse(email))
               )
  end) as email_domain_without_extension 
from test;

The CASE WHEN is used to return NULL when it's not an email (instead of an empty string). 如果CASE WHEN不是电子邮件(而不是空字符串),则用于返回NULL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM