ANSI_PADDING

  • Comments posted to this topic are about the item ANSI_PADDING

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg
  • Good question. If you had wanted to be really sneaky...

    DECLARE @Example TABLE (col1 VARCHAR(10))

    INSERT @Example VALUES ('X'), ('X' + SPACE(1)), ('X' + SPACE(2))

    SELECT COUNT_BIG(*) FROM @Example AS e WHERE col1 LIKE 'X' + SPACE(1)

    SELECT COUNT_BIG(*) FROM @Example AS e WHERE col1 LIKE N'X' + SPACE(1)

  • SQL Kiwi (12/17/2011)


    Good question. If you had wanted to be really sneaky...

    DECLARE @Example TABLE (col1 VARCHAR(10))

    INSERT @Example VALUES ('X'), ('X' + SPACE(1)), ('X' + SPACE(2))

    SELECT COUNT_BIG(*) FROM @Example AS e WHERE col1 LIKE 'X' + SPACE(1)

    SELECT COUNT_BIG(*) FROM @Example AS e WHERE col1 LIKE N'X' + SPACE(1)

    OUCH

    If everything seems to be going well, you have obviously overlooked something.

    Ron

    Please help us, help you -before posting a question please read[/url]
    Before posting a performance problem please read[/url]

  • Great question, thanks!

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

  • Thanks for the question, Jason.

    I only got it right because "3, 3, 1" was not in the answer options - that was the answer I was looking for after reading the question. I am also a bit disappointed that this part of the answer is not explained. One of the links included in the explanation includes this quote:

    When the right side of a LIKE predicate expression features a value with a trailing space, SQL Server does not pad the two values to the same length before the comparison occurs.

    That quote only reinforces my expectation that the third query should return 1. Can anyone explain to me why it doesn't?

    (And, while you're at it, you might also want to explain the even stranger behaviour demonstrated by the query Paul posted in this topic).


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Hugo Kornelis (12/19/2011)


    That quote only reinforces my expectation that the third query should return 1. Can anyone explain to me why it doesn't?

    It is 'explained' (perhaps 'documented' would be a better term) in the last paragraph of Pattern Matching in Search Conditions

    And, while you're at it, you might also want to explain the even stranger behaviour demonstrated by the query Paul posted in this topic).

    This point needs LIKE (Transact-SQL) in the sub-section 'Pattern Matching by Using LIKE'. Hilarious...

    DECLARE @Example TABLE (col1 VARCHAR(10))

    INSERT @Example VALUES ('X'), ('X' + SPACE(1)), ('X' + SPACE(2))

    DECLARE @ESC1 CHAR = '['

    DECLARE @ESC2 NCHAR = '['

    SELECT COUNT_BIG(*) FROM @Example AS e WHERE col1 LIKE 'X' + SPACE(1) ESCAPE @Esc1

    SELECT COUNT_BIG(*) FROM @Example AS e WHERE col1 LIKE 'X' + SPACE(1) ESCAPE @Esc2

  • That's a good question, thanks!

  • Thanks, Paul!

    I won't say "it all makes sense now" - but at least, I now have at least a passing chance of remembering what might have happened and where to look for an explanation if I ever encounter this counter-intuitive behaviour in the wild.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/

  • Great question.

    Got it wrong as I did not fully read the INSERT statement.

    Still, it's ok, the lesson learnt today is, always read the question properly, even on a Monday morning. 🙂

  • good one. Learn new thing !!!

    Thanks
    Vinay Kumar
    -----------------------------------------------------------------
    Keep Learning - Keep Growing !!!

  • Hugo Kornelis (12/19/2011)


    Thanks for the question, Jason.

    I only got it right because "3, 3, 1" was not in the answer options - that was the answer I was looking for after reading the question.

    I was in the same position. While I understand Paul's explanation, and the links provided, it does still seem a bit counterintuitive, as you point out in a later post.

    Thanks to OP for posting. Always good to learn something new.

    [font="Verdana"]Please don't go. The drones need you. They look up to you.[/font]
    Connect to me on LinkedIn

  • Good question.

    And a lovely additional twist from Paul.

    Tom

  • I got it right simply because I once supported an app that had a myriad of situations with and without trailing spaces on a VARCHAR column and have seen that behavior countless times. I have never been able to explain it. Still don't know if I can, even with the "explanation". It still doesn't make sense, but that's how it works...

  • Great question -- especially the quirk involved when using 'LIKE'.

    Thanks!

    Rob Schripsema
    Propack, Inc.

  • good question!!!!


    [font="Times New Roman"]rfr.ferrari[/font]
    DBA - SQL Server 2008
    MCITP | MCTS

    remember is live or suffer twice!
    the period you fastest growing is the most difficult period of your life!

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

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