How do I replace a substring of a string before a specific character?

前端 未结 4 1146
無奈伤痛
無奈伤痛 2021-01-02 19:50

Table Email:

Values:

josh@yahoo.com
carmine32@hotmail.com
zehmaneh@yahoo.com

I want to replace the string before

相关标签:
4条回答
  • 2021-01-02 20:27
    declare @t table(email varchar(30))
    insert @t values('josh@yahoo.com'),
                    ('carmine32@hotmail.com'),
                    ('zehmaneh@yahoo.com') 
    
    select stuff(email, 1, charindex('@', email), 'Test@') 
    from @t
    

    Result:

    Test@yahoo.com
    Test@hotmail.com
    Test@yahoo.com
    
    0 讨论(0)
  • 2021-01-02 20:33

    You can use SUBSTRING and CHARINDEX:

    UPDATE Email set email = 
        'test' + SUBSTRING(email, CHARINDEX('@',email), LEN(email))
    

    Fiddle: http://sqlfiddle.com/#!3/0face/6/0

    0 讨论(0)
  • 2021-01-02 20:39

    You don't even need to use substring or replace, you can use this:

    SELECT 'test' + RIGHT(email, charindex('@', REVERSE(email)))
    FROM YourTable
    

    You can test it out with this:

    DECLARE @email nvarchar(50)
    SET @email = 'carmine32@hotmail.com'
    PRINT 'test' + RIGHT(@email, charindex('@', REVERSE(@email)))
    
    0 讨论(0)
  • 2021-01-02 20:49

    You could

    select 'test' + substring(fld, charindex('@', fld), len(fld))
    
    0 讨论(0)
提交回复
热议问题