Search code examples
sqlsql-serversql-deletesql-likesql-in

SQL Server : delete where string value contains another value


I want to delete from table all records which selected string value contains in another value from another table (with ignore case sensitive).

For example: if value1="Hello" (from one table) and value2-"Hello word" (from another table), then that record should be deleted.

DELETE FROM [table1] 
WHERE value1 LIKE  '%' + (SELECT value2 FROM [table2]) + '%'

But that SQL statement returns an error.


Solution

  • You could use an exists criteria:

    delete
    from Table1 T1
    where exists (select T2.value2 from Table2 T2 where T1.value1 like '%'+T2.value2+'%')