SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Conditional Order By


Conditional Order By

Author
Message
cengland0
cengland0
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2256 Visits: 1300
mister.magoo (11/16/2010)
Thanks to those of you who appreciate this question.

To those who think it had unnecessary "tricks" in it, I did try several variants of this question and they all ended up being too easy.

The "tricks" were deliberate and designed to check whether you would notice the implicit conversion and whether you even knew it would happen.

If you don't like the question, I am sorry - for those that like to be challenged to check every small detail I think it provides a sufficient level of complexity to give you a good challenge!

Have a nice day!

I got this one right because I'm always looking for a trick. That's a general problem with the QOTD. Too many of the questions have some trick in it so when there is a straight-forward question without a trick, I find myself looking for one anyway.
Rose Bud
Rose Bud
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1793 Visits: 1062
I learned a lot from this QOTD. Count me as someone else who didn't know there was such a thing as conditional order by.

I can see the utility of this and I can see that an implicit cast could cause problems, even an error-less unintentional sort as Tony++ mentions.

Thanks to depak.a for providing a solution to the implicit cast problem.

The swapping of string1/string2 was probably an unnecessary distraction but debugging with "distractions" is a part of life. I readily grant license to QOTD authors to add tricks if they prefer that type of question.
b0b555
b0b555
Old Hand
Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)Old Hand (376 reputation)

Group: General Forum Members
Points: 376 Visits: 927
Excellent question.
Carlo Romagnano
Carlo Romagnano
SSCertifiable
SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)SSCertifiable (7.6K reputation)

Group: General Forum Members
Points: 7594 Visits: 3400
Hugo Kornelis (11/17/2010)Without the reversal, the question would have been a solid test of whether people know about the dangers of using CASE in ORDER BY, and thus an excellent question. With the reversal, it's still a good question - good, not excellent.

The QoTD was not about dangers of using CASE in ORDER BY, but the problem is in the convertion in the CASE clause.

I run on tuttopodismo
hakan.winther
hakan.winther
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3335 Visits: 612
Richard Warr (11/16/2010)
I thought it was an interesting question as well - currently more people have it wrong than right which shows that we can still be surprised at the "behind the scenes" antics of SQL Server.

Please don't describe questions as "nonsense", people take taime to set them and always have good intentions when they do so. It doesn't help your case if you can't spell "nonsense" either.


I agree with you Richard! I admire the people who dare to post questions on this site.

/Håkan Winther
MCITPBigGrinatabase Developer 2008
MCTS: SQL Server 2008, Implementation and Maintenance
MCSE: Data Platform
Hugo Kornelis
Hugo Kornelis
SSCoach
SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)SSCoach (19K reputation)

Group: General Forum Members
Points: 19033 Visits: 12426
Carlo Romagnano (11/18/2010)
Hugo Kornelis (11/17/2010)Without the reversal, the question would have been a solid test of whether people know about the dangers of using CASE in ORDER BY, and thus an excellent question. With the reversal, it's still a good question - good, not excellent.

The QoTD was not about dangers of using CASE in ORDER BY, but the problem is in the convertion in the CASE clause.

Hi Carlo,

The implicit converts in the CASE are the root cause of the danger I referred to. And while the same thing may happen in a CASE anywhere, I have thus far only seen this in ORDER BY clauses. So I guess that you and I are saying the same thing, jusst using different words to describe it.


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
mister.magoo
mister.magoo
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10837 Visits: 7891
Hugo Kornelis (11/18/2010)
Carlo Romagnano (11/18/2010)
Hugo Kornelis (11/17/2010)Without the reversal, the question would have been a solid test of whether people know about the dangers of using CASE in ORDER BY, and thus an excellent question. With the reversal, it's still a good question - good, not excellent.

The QoTD was not about dangers of using CASE in ORDER BY, but the problem is in the convertion in the CASE clause.

Hi Carlo,

The implicit converts in the CASE are the root cause of the danger I referred to. And while the same thing may happen in a CASE anywhere, I have thus far only seen this in ORDER BY clauses. So I guess that you and I are saying the same thing, jusst using different words to describe it.


The question was inspired by a forum post where someone was doing exactly that - using a CASE in the order by which would fail on implicit data type conversions. The aim was to highlight the danger of mixing data types in this way - whether it is in an ORDER by or not - but as Hugo says, it mostly happens in ORDER BY clauses.

Also, I just like it when I find something that people may not know and want to share Satisfied

MM


select geometry::STGeomFromWKB(0x0106000000020000000103000000010000000B0000001000000000000840000000000000003DD8CCCCCCCCCC0840000000000000003DD8CCCCCCCCCC08408014AE47E17AFC3F040000000000104000CDCCCCCCCCEC3F9C999999999913408014AE47E17AFC3F9C99999999991340000000000000003D0000000000001440000000000000003D000000000000144000000000000000400400000000001040000000000000F03F100000000000084000000000000000401000000000000840000000000000003D0103000000010000000B000000000000000000143D000000000000003D009E99999999B93F000000000000003D009E99999999B93F8014AE47E17AFC3F400000000000F03F00CDCCCCCCCCEC3FA06666666666FE3F8014AE47E17AFC3FA06666666666FE3F000000000000003D1800000000000040000000000000003D18000000000000400000000000000040400000000000F03F000000000000F03F000000000000143D0000000000000040000000000000143D000000000000003D, 0);




  • Forum Etiquette: How to post Reporting Services problems
  • Forum Etiquette: How to post data/code on a forum to get the best help - by Jeff Moden
  • How to Post Performance Problems - by Gail Shaw

  • Zagyg
    Zagyg
    SSCommitted
    SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

    Group: General Forum Members
    Points: 1528 Visits: 896
    Just adding my vote to the "good question" list. I'm well aware of data type conversions, and spotted the "trick", and still got it wrong through a moment of fuzzy thinking. Well done on re-highlighting the issue.
    Carla Wilson-484785
    Carla Wilson-484785
    SSCrazy
    SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

    Group: General Forum Members
    Points: 2292 Visits: 1951
    I think the "trap" is that the code "looks" like it is writing dynamic sql, but it really isn't.
    Tom Thomson
    Tom Thomson
    One Orange Chip
    One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

    Group: General Forum Members
    Points: 26289 Visits: 12506
    Although I'm irritated that the flipping of 1 and 2 was very noticeable and having noticed it I decided that was "the trick" and thought no more (thus getting the wrong answer) I still thionk it's a good question. It's very important when desk-checking someone's code or trying to debug something to do it carefully and follow through on the whole thing, not stop thinking when you spot something odd, so questions like this are valuable - they remind us that leaping to conclusions without considering all the code is a stupid thing to do.

    Tom

    Go


    Permissions

    You can't post new topics.
    You can't post topic replies.
    You can't post new polls.
    You can't post replies to polls.
    You can't edit your own topics.
    You can't delete your own topics.
    You can't edit other topics.
    You can't delete other topics.
    You can't edit your own posts.
    You can't edit other posts.
    You can't delete your own posts.
    You can't delete other posts.
    You can't post events.
    You can't edit your own events.
    You can't edit other events.
    You can't delete your own events.
    You can't delete other events.
    You can't send private messages.
    You can't send emails.
    You can read topics.
    You can't vote in polls.
    You can't upload attachments.
    You can download attachments.
    You can't post HTML code.
    You can't edit HTML code.
    You can't post IFCode.
    You can't post JavaScript.
    You can post emoticons.
    You can't post or upload images.

    Select a forum

































































































































































    SQLServerCentral


    Search