Skip to content
Advertisement

SQL replace and insert Into

I have this table “NamesTable” in SQL Server.

ID      NameEnglish      NameFrench
-----------------------------------
1       Loyalty Apple     Null
2       Home Watermelon   Null
3       Company Banana    Null

I want to insert and replace in a second column the corresponding name like this

ID      NameEnglish       NameFrench 
-----------------------------------------
1       Loyalty Apple     Loyalty Pomme
2       Home Watermelon   Home Melon 
3       Company Banana    Company Banane

This is my query:

INSERT INTO NamesTable (NameFrench)
VALUES ('Frenchreplace') 

SELECT 
    [ID], 
    NameEnglish AS [Frenchreplace], 
    REPLACE(REPLACE(REPLACE(REPLACE (NameEnglish, 'Apple', 'Pomme'),
                     'Watermelon', 'Melon'),
                     'Banana', 'Banane') AS [Frenchreplace] 
FROM 
    NamesTable

The query runs and replace the selected words, but do not insert into my existing table. What I am doing wrong?

Advertisement

Answer

No idea what is the objective of your INSERT query. But you don’t need that.

You need an UPDATE query. SELECT only retrieve from the table, it does not modify or make changes to the table.

Change your SELECT query into an UPDATE query


UPDATE N
SET    NameFrench  = REPLACE( REPLACE( REPLACE (
                                                NameEnglish, 'Apple', 'Pomme'),
                                       'Watermelon', 'Melon'),
                             'Banana', 'Banane')
FROM   NamesTable AS N

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement