[英]How to select only the characters appearing before a specific symbol in a SQL Select statement
I have strings in a database like this: 我在这样的数据库中有字符串:
firstname.lastname@email.com/IMCLientName
And I only need the characters that appear before the @ symbol. 我只需要在@符号之前出现的字符。
I am trying to find a simple way to do this in SQL. 我试图找到一种在SQL中执行此操作的简单方法。
DECLARE @email VARCHAR(100)
SET @email = 'firstname.lastname@email.com/IMCLientName'
SELECT SUBSTRING(@email,0, CHARINDEX('@',@email))
Building on Ian Nelson 's example we could add a quick check so we return the initial value if we don't find our index. 在Ian Nelson的例子的基础上,我们可以添加一个快速检查,如果我们找不到索引,则返回初始值。
DECLARE @email VARCHAR(100)
SET @email = 'firstname.lastname.email.com/IMCLientName'
SELECT CASE WHEN CHARINDEX('@',@email) > 0
THEN SUBSTRING(@email,0, CHARINDEX('@',@email))
ELSE @email
END AS email
This would return 'firstname.lastname.email.com/IMCLientName'. 这将返回'firstname.lastname.email.com/IMCLientName'。 If you used 'firstname.lastname@email.com/IMCLientName' then you would receive 'firstname.lastname' as a result. 如果您使用'firstname.lastname@email.com/IMCLientName',那么您将收到'firstname.lastname'作为结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.