December 17, 2011 at 1:28 pm
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. SelburgDecember 17, 2011 at 3:32 pm
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)
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
December 18, 2011 at 9:33 am
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
December 19, 2011 at 12:45 am
Great question, thanks!
Need an answer? No, you need a question
My blog at https://sqlkover.com.
MCSE Business Intelligence - Microsoft Data Platform MVP
December 19, 2011 at 12:53 am
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).
December 19, 2011 at 1:19 am
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
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
December 19, 2011 at 1:23 am
That's a good question, thanks!
December 19, 2011 at 1:34 am
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.
December 19, 2011 at 2:51 am
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. 🙂
December 19, 2011 at 5:32 am
good one. Learn new thing !!!
Thanks
Vinay Kumar
-----------------------------------------------------------------
Keep Learning - Keep Growing !!!
December 19, 2011 at 7:04 am
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
December 19, 2011 at 7:07 am
Good question.
And a lovely additional twist from Paul.
Tom
December 19, 2011 at 7:46 am
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...
December 19, 2011 at 10:37 am
Great question -- especially the quirk involved when using 'LIKE'.
Thanks!
Rob Schripsema
Propack, Inc.
December 19, 2011 at 12:10 pm
good question!!!!
Viewing 15 posts - 1 through 15 (of 25 total)
You must be logged in to reply to this topic. Login to reply