Wildcards

  • Comments posted to this topic are about the item Wildcards

  • Nice straight forward question. Thanks

    Hope this helps...

    Ford Fairlane
    Rock and Roll Detective

  • Easy one. Thanks Steve.

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • To nitpick.

    For '[]' to be valid as an answer SQL should have allowed something like

    mychar LIKE '[]dam'

    But this offcourse doesnt work. '[]' only work in conjunction with other input. In this case 'a-z'... which offcourse means that if one of the examples had started with a "1" for instance... it would not have been found. Also some international characters wouldnt have worked with 'a-z' either.

    Whereas '_' works as is.

    /T

  • Good question. Man, I have not used the "_" in a while so I almost got it wrong. Thankfully my brain kicked in.:-D

    Thanks for the question.

  • This was removed by the editor as SPAM

  • Good question. Thanks Steve.

  • steve.jacobs (11/21/2013)


    Good question. Man, I have not used the "_" in a while so I almost got it wrong. Thankfully my brain kicked in.:-D

    Thanks for the question.

    +1 Same here. I really had dive deep into the memory banks on this one.



    Everything is awesome!

  • Nice and easy. Thanks, Steve!

  • Nice question....

  • I gave the example a try, mychar LIKE '[a-z]dam' returns only Bdam. While mychar like '[A-Z]dam' returns Adam and Bdam. Is this a result of the version/flavor of SQL?

  • Bad question because the words you were wrote don't necessarily mean what you apparently think they do. Or was it intentionally a trick question?

    Mind you, it's not as bad as Tuesday's question, which is unambiguously wrong and ought to be corrected (there are only about a dozen comments so far; they all say it's wrong;

    which of these wildcard characters should be followed by a list of wildcard characters; but the list contains &, which isn't a wildcard character, {} which isn't a wild card character (it's two characters), and [] which isn't a wild card character (it too is two characters). You might want to say that you didnt mean a list of single wildcard characters, but if you mant a list of strings of wildcard characters then the presence of {} and & means the list doesn't match the question; doesn't match anything at all makes the answer wrong. So we are left guessing what you mean. I guessed you meant strings of characters, since you gave a list of strings and not a list of characters or a list of wildcard strings. And the string [] can't match a single character - indeed it can't match anything at all unless preceeded by an escape character and then it matches 2 characters.

    [] can't match a single character: s like '[]' is always false (unless s is null, in which case it's unknown).

    {} can't natch a single character, it always matches two.

    _ matches any single character, and can only match a single character.

    & matches a particular single character (itself), and can't match anything else.

    % matches any single character, but also matches any string.

    So there are three strings which can match a single character; but only two strings are to be selected; two of them can only match a single character, the third can match other things as well, so the choice is obvious: the right answer is _ and &.

    Tom

  • Tom,

    You're being pedantic here in terms of the question. The question isn't which of these are single characters, which which wildcards match.

    [] is a set of wildcard characters. This can be used to match a single character. You do need to provide a character to match, but this does work.

    ; with mycte (mychar)

    as

    (select mychar = 'Steve'

    union

    select mychar = 'Bill'

    union

    select mychar = 'Stephanie'

    union

    select mychar = 'Adam'

    )

    select mychar

    from mycte

    where mychar like '%'

    "&" isn't a wildcard, therefore it doesn't count.

    % doesn't match single characters, it matches multiple characters. There could be a reasonable argument here that if the data contains a single character, this matches, but in general, this isn't designed to, nor is code written with this to, match a single character. I'll reword the question to remove this.

    You are treating these as literals, not as the wildcard characters.

  • For me, I am always using % in LIKE statements, although there is a minor difference which is:

    % means 0 or more characters in this position.

    _ means 1 character only @ that position.

    http://msdn.microsoft.com/en-us/library/069b0htd(v=vs.110).aspx"> http://msdn.microsoft.com/en-us/library/069b0htd(v=vs.110).aspx

    hmmm, interesting.

    Thanks & Best Regards,
    Hany Helmy
    SQL Server Database Consultant

  • nice and easy..

    Thanks Steve..

Viewing 15 posts - 1 through 15 (of 25 total)

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