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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s