How to select only characters that appear before a specific character in a SQL Select statement

I have rows in the database:

firstname.lastname@email.com /IMCLientName 

And I only need the characters that appear before the @ character.

I am trying to find an easy way to do this in SQL.

+7
source share
2 answers
 DECLARE @email VARCHAR(100) SET @email = ' firstname.lastname@email.com /IMCLientName' SELECT SUBSTRING(@email,0, CHARINDEX('@',@email)) 
+19
source

Based on Jan Nelson , we could add a quick check to return the original value if our index is not found.

 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 will return 'firstname.lastname.email.com/IMCLientName'. If you used " firstname.lastname@email.com / IMCLientName", then you would get "firstname.lastname".

0
source

All Articles