Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase «««12345»»

Conditional Order By Expand / Collapse
Author
Message
Posted Wednesday, November 17, 2010 3:45 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 24, 2013 9:59 PM
Points: 1,354, Visits: 1,299
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.
Post #1022030
Posted Wednesday, November 17, 2010 7:41 AM


UDP Broadcaster

UDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP BroadcasterUDP Broadcaster

Group: General Forum Members
Last Login: Friday, September 19, 2014 7:15 AM
Points: 1,447, Visits: 1,059
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.

Post #1022157
Posted Wednesday, November 17, 2010 5:14 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: 2 days ago @ 9:58 PM
Points: 330, Visits: 623
Excellent question.
Post #1022549
Posted Thursday, November 18, 2010 1:37 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 7:33 AM
Points: 2,542, Visits: 2,410
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.
Post #1022666
Posted Thursday, November 18, 2010 2:06 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 1:38 AM
Points: 2,582, Visits: 553
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
MCITP:Database Developer 2008
MCTS: SQL Server 2008, Implementation and Maintenance
MCSE: Data Platform
Post #1022680
Posted Thursday, November 18, 2010 2:10 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 4:48 PM
Points: 6,021, Visits: 8,290
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
Post #1022685
Posted Thursday, November 18, 2010 2:30 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: 2 days ago @ 3:53 PM
Points: 1,780, Visits: 5,750
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


MM


  • MMGrid Addin
  • MMNose Addin


  • 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

  • Post #1022697
    Posted Friday, November 19, 2010 3:11 AM


    Ten Centuries

    Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

    Group: General Forum Members
    Last Login: Yesterday @ 4:05 AM
    Points: 1,353, Visits: 753
    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.
    Post #1023376
    Posted Friday, November 19, 2010 8:52 AM
    SSCommitted

    SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

    Group: General Forum Members
    Last Login: Monday, September 29, 2014 7:01 AM
    Points: 1,571, Visits: 1,852
    I think the "trap" is that the code "looks" like it is writing dynamic sql, but it really isn't.
    Post #1023592
    Posted Sunday, November 21, 2010 1:42 PM


    SSCertifiable

    SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

    Group: General Forum Members
    Last Login: Yesterday @ 4:57 PM
    Points: 7,745, Visits: 9,493
    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
    Post #1024073
    « Prev Topic | Next Topic »

    Add to briefcase «««12345»»

    Permissions Expand / Collapse