我需要你的支持,今天我得到了一个我需要这样做的场景。
该场景是包含用户名和ID的电子邮件,需要拆分并在两个不同的列中插入表中,即用户ID,用户名。
ex: ABC123@xyz.com, AAACC2356@mnc.com
ABC to be insert in table under username column and 123 to be insert in user id column
谢谢
纳伦德拉
答案 0 :(得分:1)
你可以试试这个:
DECLARE @Email VARCHAR(100)= 'sample12312@test.com'
SET @Email = STUFF(@Email,CHARINDEX('@',@Email),LEN(@Email), '')
SELECT SUBSTRING(@Email, 1, PATINDEX('%[0-9]%',@Email)-1) AS Username,
SUBSTRING(@Email,PATINDEX('%[0-9]%',@Email), LEN(@Email)) AS UserID
OR
SELECT SUBSTRING(@Email, 1, PATINDEX('%[0-9]%',@Email)-1) AS Username,,
SUBSTRING(STUFF(@Email,CHARINDEX('@',@Email),LEN(@Email), ''), PATINDEX('%[0-9]%',@Email), LEN(@Email)) AS UserID
答案 1 :(得分:0)
您可以使用少量功能来实现:
DECLARE @exp VARCHAR(1000) = 'AAACC2356@mnc.com'--'ABC123@xyz.com'
--finds where numbers starts
DECLARE @SplitPoint INT = (SELECT PATINDEX ( '%[0-9]%' , @exp ))
--finds where is @ sign
DECLARE @etaPoint INT = (SELECT PATINDEX ( '%@%' , @exp ))
--gets username
SELECT SUBSTRING(@exp, 0, @SplitPoint)
--gets number
SELECT SUBSTRING(@exp, @SplitPoint, @etaPoint-@SplitPoint)
答案 2 :(得分:0)
Declare @X varchar(100)
Select @X= 'AAACC2356@mnc.com'
Select @X= SubString(@X,PATINDEX('%[0-9]%',@X),Len(@X))
Select @X= SubString(@X,0,PATINDEX('%[^0-9]%',@X))
Select @X
Declare @Y varchar(100)
Select @Y = 'AAACC2356@mnc.com'
Select @Y= SubString(@Y,PATINDEX('%[A-Z]%',@Y),Len(@Y))
Select @Y= SubString(@Y,0,PATINDEX('%[^A-Z]%',@Y))
Select @Y