JoeDumars
JoeDumars

Reputation: 23

MS SQL - find out if a field exists within another

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"?

Upvotes: 1

Views: 150

Answers (2)

JoeDumars
JoeDumars

Reputation: 23

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

Upvotes: 0

Mureinik
Mureinik

Reputation: 311438

One option is to use charindex:

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

Upvotes: 1

Related Questions