> home
> how we work
> who we're working for
> about us
> publications
> buy apache essentials
> client login

play
static·red
edmonton and area web development

Current Projects

Edmonton Weather


>>Le blog.

speakeasy archives


SQL Query

<< Can you spot the problem? | Main | Rank amateur >>

I just fought with this for a little while, so this is here for my own reference:'

To order a returned dataset from a MySQL database, ignoring "A", "An", or "The" when sorting, you need to use the following query (assume that 'title' is the column you want to sort by):

SELECT * ,
  IF (
    left( title, 2 ) = 'a ' OR left( title, 4 ) = 'the ',
    IF (
    left( title, 2 ) = 'a', right( title, length( title ) -2 ) , right( title, length( title ) -4 )
  ), title
) AS at
FROM primarytexts

Posted by Darren James Harkness on Sunday, October 3, 2004 08:38 PM
Trackbacks...


Comments:


  1. Hey Darren,

    I'm not sure, but I think MySQL has some flavour of SUBSTRING() that may make this a bit easier.

    -- Posted by >> Wim » Tuesday, October 5, 2004 04:54 PM
  2. This is actually a slightly better version:

    SELECT * ,
    IF (
    left( title, 2 ) = 'a ' OR left( title, 4 ) = 'the ' OR left( title, 3 ) = 'an',
    IF (
    left( title, 2 ) = 'a' OR left( title, 2 ) = 'an', right( title, length( title ) -2 ) , right( title, length( title ) -4 )
    ), title
    ) AS sorttitle
    FROM primarytexts
    ORDER BY sorttitle ASC

    -- Posted by >> Darren » Wednesday, October 27, 2004 11:42 PM

Post a comment









Remember personal info?


Comments:


* under no circumstances will your email address be traded for a sack of quarters. No-sirree.