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

后端 未结 4 670
我在风中等你
我在风中等你 2021-01-30 20:07

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

I\'m looking to do this:

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


        
相关标签:
4条回答
  • 2021-01-30 20:15

    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%'
    
    0 讨论(0)
  • 2021-01-30 20:16
        IF CHARINDEX('TextToSearch',@TextWhereISearch, 0) > 0 => TEXT EXISTS
    
        IF PATINDEX('TextToSearch', @TextWhereISearch) > 0 => TEXT EXISTS
    
        Additionally we can also use LIKE but I usually don't use LIKE.
    
    0 讨论(0)
  • 2021-01-30 20:31

    CONTAINS is for a Full Text Indexed field - if not, then use LIKE

    0 讨论(0)
  • 2021-01-30 20:38

    Instead of LIKE (which does work as other commenters have suggested), you can alternatively use CHARINDEX:

    declare @full varchar(100) = 'abcdefg'
    declare @find varchar(100) = 'cde'
    if (charindex(@find, @full) > 0)
        print 'exists'
    
    0 讨论(0)
提交回复
热议问题