Wildcard Searches

  • standardbluecaboose (9/29/2015)


    Thanks for the quick reply.

    The WHERE ' ' + Name + ' ' LIKE '%[^A-Za-z]Richard[^A-Za-z]%'; is insane in a bad way. I have no suggestions for how to make it better, but it feels like such a hack. However, a lot about SQL feels like a hack to me.

    Thank you for the update about the PATINDEX.

    I will say that Column LIKE '%X%'` and `PATINDEX('X', Column) both do a full scan.

    Sure, it feels like a hack, but sometimes you have to do nasty things for the code to work.

    And yes, the only way to get an index seek is when searching in the "Starts with" formula. By the way, your PATINDEX is missing something to work, it should be PATINDEX('%X%', Column)

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares (9/29/2015)


    Alan.B (9/29/2015)


    Great article Luis! Informative, to the point. I picked up a couple things.

    Interesting technique using REPLICATE, I would add that I like to use CHAR() more often in my searches. E.g CHAR(32) for spaces, CHAR(9) for tabs when I'm using LIKE, PATINDEX or CHARINDEX. Much easier to distinguish than the literal version of each.

    There are lots of things that can be done with patterns, like a pattern based splitter.

    That would make for a great article 😉

    Interesting, I prefer the single space over the CHAR(32) but the CHAR(9) over the tab literal. Maybe I'd just have to get used to it.

    About the article, you mean writing about the things that can be done? or the splitter? because the splitter already has an article written by Dwain.

    Things that can be done. I've seen so many examples over the years and it seams like a great compliment to the original article. Was just thinking out loud.

    "I cant stress enough the importance of switching from a sequential files mindset to set-based thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code."

    -- Itzik Ben-Gan 2001

  • Thanks for the article. This provides good background and some tweaks I didn't think of upon which I can build.

  • Great article! Thank you.


    The "Numbers" or "Tally" Table: What it is and how it replaces a loop[/url]

    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs[/url]

  • Hi Luis,

    Another Approach for "Like" Search is to Use "DataLength" And "Replace" together, as shown below:

    Begin

    declare @lv_search_string varchar(100);

    select @lv_search_string ='Parker'

    SELECT * FROM dbo.LIKETest WHERE DATALENGTH(Name) <> DATALENGTH(REPLACE( NAME, @lv_search_string, '' ))

    End

    Go

    Logic As Follows:

    If search string is found in data, replacing it with empty String and then calculation of data length will never be equal to the actual Data Length of the Data.

    This One Query supports 3 use cases:

    --F) Ends with ‘some string

    SELECT * FROM dbo.LIKETest WHERE Name LIKE '%Parker';

    --G) Contains ‘some string’

    SELECT * FROM dbo.LIKETest WHERE Name LIKE '%Richard%';

    --H) Contains ‘exact word’

    SELECT * FROM dbo.LIKETest WHERE Name LIKE '% Richard %';

    I do not have the performance stats but,It was Better than Regular Like Search. Can Anyone Provide the stats using large volume of data..??

  • Thanks for the great refresher course on wildcards.

  • Peddi Praveen kumar (9/30/2015)


    Hi Luis,

    Another Approach for "Like" Search is to Use "DataLength" And "Replace" together, as shown below:

    Begin

    declare @lv_search_string varchar(100);

    select @lv_search_string ='Parker'

    SELECT * FROM dbo.LIKETest WHERE DATALENGTH(Name) <> DATALENGTH(REPLACE( NAME, @lv_search_string, '' ))

    End

    Go

    Logic As Follows:

    If search string is found in data, replacing it with empty String and then calculation of data length will never be equal to the actual Data Length of the Data.

    This One Query supports 3 use cases:

    --F) Ends with ‘some string

    SELECT * FROM dbo.LIKETest WHERE Name LIKE '%Parker';

    --G) Contains ‘some string’

    SELECT * FROM dbo.LIKETest WHERE Name LIKE '%Richard%';

    --H) Contains ‘exact word’

    SELECT * FROM dbo.LIKETest WHERE Name LIKE '% Richard %';

    I do not have the performance stats but,It was Better than Regular Like Search. Can Anyone Provide the stats using large volume of data..??

    I tested on a million row table with and without an index on the column and the performance was similar on both options. On some runs the LIKE would be faster and in others it would be slower. And I'm taking about 2% faster or slower.

    On the other hand, that approach is only equivalent to the "Contains ‘some string’" approach. In the others, it would return incorrect results.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • This article is just another example of why lack of native "modern" regex support is holding back MS SQL.

    replicate('_',fixedLength) ?

    .{fixedLength}

    .{min,max} ?

    ... like replicate('_',...) or ... like replicate ('_',...) or ... like replicate('_',...) ... ... ...

    And that's the tip of the iceberg.

    1/100th of the functionality is already there with LIKE patterns--make the leap to variable length and alternative sub-pattern matches already! (with a syntax that everyone should already be familiar with)

    With an understanding of how regex matches are performed (greediness, possessiveness (PCRE), anchoring, etc.), many potential performance issues could be avoided. Those that can not be avoided are a problem wherever they arise.

  • Thanks for your article.

    the query for Contains ‘exact word’ will not work if the searched word is the last word on a line.

  • quagmired (10/1/2015)


    This article is just another example of why lack of native "modern" regex support is holding back MS SQL.

    You're probably right. I'm not sure what prevents MS to include regex support on SQL Server, but it would be great to have it without using CLR.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Yakov Shlafman (10/3/2015)


    Thanks for your article.

    the query for Contains ‘exact word’ will not work if the searched word is the last word on a line.

    Thank you, that's actually addressed in the article and shows the solution for that issue.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Great article. And I love the Sesame Street reference 😀

  • You say using CHARINDEX or PATINDEX for testing for the occurrence of a character string is a bad idea.

    that's why i read this article.

    Could not find more of an argument than trailing spaces, which is not much of an argument as trailing spaces hardly will contain the search string.

    so, what's the reason ?

  • I enjoyed reading your article, reminding me of some of the odd nuances of pattern matching.

  • Christy M (11/18/2016)


    Great article. And I love the Sesame Street reference 😀

    I'm glad that you liked it. Thank you for the feedback.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 15 posts - 16 through 30 (of 38 total)

You must be logged in to reply to this topic. Login to reply