SQL search multiple values in same field

后端 未结 4 1311
北恋
北恋 2020-11-30 04:48

I\'m building a simple search algorithm and I want to break my string with spaces, and search my database on it, like so:

$         


        
4条回答
  •  误落风尘
    2020-11-30 05:23

    This will works perfectly in both cases, one or multiple fields searching multiple words.

    Hope this will help someone. Thanks

    declare @searchTrm varchar(MAX)='one two three four'; 
    --select value from STRING_SPLIT(@searchTrm, ' ') where trim(value)<>''
    select * from Bols 
    WHERE EXISTS (SELECT value  
        FROM STRING_SPLIT(@searchTrm, ' ')  
        WHERE 
            trim(value)<>''
            and(    
            BolNumber like '%'+ value+'%'
            or UserComment like '%'+ value+'%'
            or RequesterId like '%'+ value+'%' )
            )
    

提交回复
热议问题