I have the following scenario John Doe johndoe@email.com john johndoe@email.com
I want away that I can detect the first right space and just exclude everything to the left so I just get the email address of the person. So: John Doe johndoe@email.com should be johndoe@email.com john johndoe@email.com should be johndoe@email.com
This is what i have
Declare @test varchar(50)
Select @test = 'John Doe johndoe@email.com'
SELECT Right(@test, CHARINDEX(' ', @test))
This is only giving me the email.com!
Thank you.
Declare @test varchar(50)
Select @test = 'John Doe johndoe@email.com'
SELECT RIGHT(@test, CHARINDEX(' ', REVERSE(@test)-1))
or a safer approach (if there are strings without space separator):
Declare @test varchar(50)
Select @test = 'johndoe@email.com'
SELECT
CASE
WHEN CHARINDEX(' ', REVERSE(@test)) > 0 THEN RIGHT(@test, CHARINDEX(' ', REVERSE(@test))-1)
ELSE @test
END