SQL – Check If One String Contains Another String (CHARINDEX)

The CHARINDEX() function returns the location of a search string within another string.

For Example:

This code will return 11, which equates to the position of ‘sentance’ within the string ‘This is a sentance’.

select charindex(‘sentance’,’This is a sentance’)

This can be used to check if string A is contained in String B by using the following command:

select * from TABLE
where¬†charindex(‘ROB’,NAME) != 0

The above command will return rows from the database where the field NAME contains the string ‘ROB’.

Advertisements