SQL LIKE wildcard space character

前端 未结 9 1250
庸人自扰
庸人自扰 2020-12-19 04:28

let\'s say I have a string in which the words are separated by 1 or more spaces and I want to use that string in and SQL LIKE condition. How do I make my SQL and tell it to

相关标签:
9条回答
  • 2020-12-19 05:08

    if the condition:

    WHERE myField LIKE '%Hello world%'
    

    doesn't work try

    WHERE myField LIKE '%Hello%' 
    

    and

    WHERE myField LIKE '%world%'
    

    this approach is helpful in a few specific use cases, hope this helps.

    0 讨论(0)
  • 2020-12-19 05:11

    I know this is late, but I never found a solution to this in relation to a LIKE question.

    There is no way to do what you're wanting within a SQL LIKE. What you would have to do is use REGEXP and [[:space:]] inside your expression.

    So to find one or more spaces between two words..

    WHERE col REGEXP 'firstword[[:space:]]+secondword'
    
    0 讨论(0)
  • 2020-12-19 05:12

    I just replace the whitespace chars with '%'. Lets say I want to do a LIKE query on a string like this 'I want to query this string with a LIKE'

    @search_string = 'I want to query this string with a LIKE'
    @search_string = ("%"+@search_string+"%").tr(" ", "%")
    @my_query = MyTable.find(:all, :conditions => ['my_column LIKE ?', @search_string])
    

    first I add the '%' to the start and end of string with

    ("%"+@search_string+"%")

    and then replace other remaining whitespace chars with '%' like so

    .tr(" ", "%")

    0 讨论(0)
提交回复
热议问题