MS SQL - find out if a field exists within another

Viewed 53

I have a table that, among its columns, has two particular char columns: "Code" and "Obs".

For example:

Column1 Code Obs
aaa 123 This customer's code is 123

How do I find this record, based on the fact that the info on column "Code" is present in the info on column "Obs"?

2 Answers

One option is to use charindex:

SELECT *
FROM   mytable
WHERE  CHARINDEX(code, obs) > 0

Is there a reason this is happening?

SELECT num1, num2, CHARINDEX(num1, num2) as charindex
FROM   mytable
Num1 Num2 charindex
3456 123-456 0
3579 135790 0
35 35 1
35 351 0
Related