How to show only part of a string in sql server?

0

I have a table and in this one, a post column. What I need is for a query to show only the domains, that is, hotmail.com , gmail.com , etc ..

SELECT  correo FROM cliente
    
asked by ingswsm 01.03.2018 в 22:06
source

1 answer

2

We can take advantage of @ as separator character, and stay with the word to the right of it. We could do it like this:

DECLARE @correo  VARCHAR(255)
SELECT  @correo = '[email protected]'

SELECT SUBSTRING(@correo, CHARINDEX('@', @correo)+1,LEN(@correo))

----------
"mail.com"

We searched with CHARINDEX() the position of @ and cut from that place + 1 to the end of the chain with SUBSTRING() . Taking it to your example would be:

SELECT  SUBSTRING(correo, CHARINDEX('@', correo)+1,LEN(correo))
        FROM cliente
    
answered by 01.03.2018 в 22:16