SQL LIKE wildcard space character

前端 未结 9 1249
庸人自扰
庸人自扰 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 04:53

    If your dialect allows it, use SIMILAR TO, which allows for more flexible matching, including the normal regular expression quantifiers '?', '*' and '+', with grouping indicated by '()'

    where entry SIMILAR TO 'hello +there'
    

    will match 'hello there' with any number of spaces between the two words.

    I guess in MySQL this is

    where entry RLIKE 'hello +there'
    
    0 讨论(0)
  • 2020-12-19 04:57

    you can't do this using LIKE but what you can do, if you know this condition can exist in your data, is as you're inserting the data into the table, use regular expression matching to detect it up front and set a flag in a different column created for this purpose.

    0 讨论(0)
  • 2020-12-19 04:58

    I think that the question is not asking to match any spaces but to match two strings one a pattern and the other with wrong number of spaces because of typos.

    In my case I have to check two fields from different tables one preloaded and the other filled typed by users so sometimes they don't respect 100% the pattern.

    The solution was to use LIKE in the join

       Select table1.field
        from table1
        left join table2 on table1.field like('%' + replace(table2.field,' ','%')+'%')
    
    0 讨论(0)
  • 2020-12-19 05:02

    http://www.techonthenet.com/sql/like.php

    The patterns that you can choose from are:

    % allows you to match any string of any length (including zero length)

    _ allows you to match on a single character

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

    If you're just looking to get anything with atleast one blank / whitespace then you can do something like the following WHERE myField LIKE '% %'

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

    Another way to match for one or more space would be to use []. It's done like this.

    LIKE '%[ ]%'
    

    This will match one or more spaces.

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