【问题标题】:I want to get the string which is before @ and after any other special character or blank space in SQL Server我想在 SQL Server 中获取 @ 之前和任何其他特殊字符或空格之后的字符串
【发布时间】:2020-09-01 12:37:24
【问题描述】:

我想在 SQL 服务器中拆分字符串并获取 @ 之前和任何其他特殊字符之后的值。

Example 1: I have a string     
abc.123.vet@xyz.com     
Expected output: vet

Example 2: aaa@yy.com  
Expected output: aaa

Example 3: aaa bte@yy.com  
Expected output: bte

我试过了:

代码 sn-p #1:

DECLARE @email VARCHAR(100)
SET @email = 'firstname.lastname@email.com'

SELECT SUBSTRING(@email, 0, CHARINDEX('@', @email))

代码 sn-p #2:

DECLARE @emai1l VARCHAR(100)
SET @emai1l = 'first.name.lastname@email.com'

SELECT SUBSTRING(SUBSTRING(@emai1l, 0, CHARINDEX('@', @emai1l)), 0, CHARINDEX('.', @emai1l))

【问题讨论】:

  • 您将什么定义为“特殊字符”?任何不是字母数字的东西?
  • 是的,非字母数字字符。

标签: sql sql-server regex split


【解决方案1】:

SQL Server 不是这种类型的字符串操作的最佳选择。但是,这是一种方法:

select t.str, v.str,
       right(v.str,
             patindex('%[, .]%', reverse(v.str) + ' ') - 1
            ) as result
from (values ('abc.123.vet@xyz.com'), ('aaa@yy.com'), ('aaa bte@yy.com')
     ) t(str) cross apply
     (values (left(t.str, charindex('@', t.str) - 1))
     ) v(str) ;

Here 是一个 dbfiddle。

【讨论】:

  • 感谢您的建议。我想获取 vet 作为示例 1 的输出:abc.123.vet@xyz.com
  • @ShreyaS 。 . .这是对逻辑的简单调整。我确定了答案。
  • 感谢您的帮助。
猜你喜欢
  • 2019-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 1970-01-01
  • 1970-01-01
  • 2016-02-12
  • 2022-11-10
相关资源
最近更新 更多