SQL SERVER - Substring

Asked

Viewed 970 times

0

Good morning

Sirs

I would like to know how to select a value within a table where it starts with a certain value

For example :

Campoemail [email protected] [email protected] [email protected]

I just want to get the values of the @forward .

Got any Function that does that ?

2 answers

1

to do this, simply calculate the position of the @ and the size of the string, below is a simple and testable example:

DECLARE @email varchar(60);
SELECT @email = '[email protected]';
select SUBSTRING(@email, charindex('@',@email),LEN(@email))

If you want to remove the @ of the answer, just add 1 in the charindex:

select SUBSTRING(@email, charindex('@',@email)+1,LEN(@email)) 

0

Luiz, see if it helps:

declare @document varchar(64),  @charinicio int = 0
select @document = '[email protected]';  
set @charinicio = (select  CHARINDEX('@', @document))
select SUBSTRING(@document, @charinicio+1, len(@document))

Result: 'dominio.com.br'

  • Thank you @Degas, it worked perfectly here ;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.