10

I am trying to format the email address in my table by removing everything starting the @. Also I would like to replace the underscore with blank space.

For example: [email protected]

I would like the above email to be changed like this: FirstName LastName

Here is my code but this trims everything after the @ and that is what i want. But how can i replace the underscore with blank. I want all in one statement using the update function. How can I do that?

SELECT 
     left (Email, CHARINDEX('@',Email)-1)
  FROM [Dashboard]

Thanks for the help

2 Answers 2

20
SELECT REPLACE(LEFT(Email, CHARINDEX('@',Email)-1),'_',' ')
FROM [DSR].[dbo].[RCA_Dashboard]
0
1

This can be helpful if you need to remove all after the last certain character:

Declare @String nvarchar(max) = 
  'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\log.ldf'


select reverse(substring(reverse (@String), CHARINDEX('\', reverse (@String))+1, len(reverse (@String))));
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.