Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a VARCHAR variable contains a substring?

I thought it was CONTAINS, but that's not working for me.

I'm looking to do this:

IF CONTAINS(@stringVar, 'thisstring')    ... 

I have to run one select or another, depending on whether that variable contains a string and I can't figure out how to get it to work. All the examples I'm seeing are using columns in the contains.

Thanks in advance.

like image 535
Yatrix Avatar asked Sep 04 '12 14:09

Yatrix


People also ask

How do you check if a string contains a substring?

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.

How do you check whether a string contains a substring in SQL?

Method 1 - Using CHARINDEX() function This function is used to search for a specific word or a substring in an overall string and returns its starting position of match. In case no word is found, then it will return 0 (zero).

How do you check if a variable contains a substring in Bash?

To check if a string contains a substring in Bash, use comparison operator == with the substring surrounded by * wildcards.


1 Answers

The standard SQL way is to use like:

where @stringVar like '%thisstring%' 

That is in a query statement. You can also do this in TSQL:

if @stringVar like '%thisstring%' 
like image 148
Gordon Linoff Avatar answered Sep 23 '22 21:09

Gordon Linoff