Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update column if another column contains a string

I have a table that contains three columns.

column1        column2     column3 
mytestdata     test 
myotherdata    test 

I want to insert 'somestring' into column3 if column1 contains the value in column2

The result would look like:

column1        column2     column3 
mytestdata     test        'somestring' 
myotherdata    test 
like image 784
Blaze Avatar asked Dec 11 '25 15:12

Blaze


2 Answers

SQL Server:

UPDATE myTable
   SET column3 = 'somestring'
 WHERE column1 LIKE '%' + column2 + '%'
like image 105
n8wrl Avatar answered Dec 14 '25 09:12

n8wrl


SQL Server:

UPDATE theTable SET column3 = 'somestring' 
WHERE CHARINDEX (column2, column1) > 0
like image 23
egrunin Avatar answered Dec 14 '25 09:12

egrunin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!