^ : T-SQL

  • ronmoses (11/20/2009)


    I got tripped up on the fact that the string didn't end with %. So I answered the question for 'Jo[^n]%' instead of 'Jo[^n]'. Oops.

    Add me to this list. Read the question and answered too quickly. Missed the fact the % wasn't listed. :blink:

    David

  • The answer, based on LIKE, is that you are looking for 3 character matches without the %.

    So you have possible:

    John

    Jonathan

    Jon

    Joe

    Jack

    Of these, the 3 character ones are:

    Jon

    Joe

    Of those, the pattern "JO[^n]" is matched only by Joe. Jon has an "n" in the third position and the caret (^) means do not match.

    If we had added the % at the end, "JO[^n]%", then "John" would have matched as well. "Jonathan would not have matched with an "n" in the third position.

  • Thank You.:-)

  • Nice explanation. Thanks

    Steve Jones - Editor (11/20/2009)


    The answer, based on LIKE, is that you are looking for 3 character matches without the %.

    So you have possible:

    John

    Jonathan

    Jon

    Joe

    Jack

    Of these, the 3 character ones are:

    Jon

    Joe

    Of those, the pattern "JO[^n]" is matched only by Joe. Jon has an "n" in the third position and the caret (^) means do not match.

    If we had added the % at the end, "JO[^n]%", then "John" would have matched as well. "Jonathan would not have matched with an "n" in the third position.

  • I'm pretty sure the '^' character is a caret, not a carot 🙂

  • Try this

    select * from @table where [name] like 'Jo_'

  • Steve Jones - Editor (11/20/2009)


    The answer, based on LIKE, is that you are looking for 3 character matches without the %.

    So you have possible:

    John

    Jonathan

    Jon

    Joe

    Jack

    Of these, the 3 character ones are:

    Jon

    Joe

    Of those, the pattern "JO[^n]" is matched only by Joe. Jon has an "n" in the third position and the caret (^) means do not match.

    If we had added the % at the end, "JO[^n]%", then "John" would have matched as well. "Jonathan would not have matched with an "n" in the third position.

    Wouldn't it have been nice if the answer's explanation had given this detail, or at the very least made the point that [^n] matches only a single character? The imaginary "%" on the end of the pattern mentioned bin a couple of comments describes exactly where I went wrong, don't know why because I should be on top of this given how much I've used it) but the explanations should surely try a bit harder to educate.

    Tom

Viewing 7 posts - 16 through 21 (of 21 total)

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