Gayathri L
Gayathri L

Reputation: 1487

Dynamically update some part of text in SQL Server column content

I have tried this script to update by removing some part of text in the description field in SQL Server:

UPDATE products
SET description = LEFT(description, CHARINDEX('<b>Please select xxxx</b>', description) - 1)
WHERE productid = 'abc'

It works fine, but I am unable to update it dynamically to the all the products.

Upvotes: 0

Views: 484

Answers (2)

Rex
Rex

Reputation: 2140

probably use case-when condition:

UPDATE  products
SET     Description = REPLACE(Description,
                       CASE WHEN ProductId = 1 THEN 'Your pattern for product 1'
                            WHEN ProductId = 2 THEN 'Your Pattern for Product 2'
                            -- WHEN ProductId = 3 THEN ...
                            ELSE '' -- Default empty
                       END, '');

--- only if you are sure there are not too many products :)

Upvotes: 0

gvee
gvee

Reputation: 17171

UPDATE products
SET    description = Replace(description, '<b>Please select xxxx</b>', '')
WHERE  description LIKE '%<b>Please select xxxx</b>%';

Upvotes: 3

Related Questions