Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Group by, ignore common words

Status
Not open for further replies.

MRKisThatKid

Technical User
Mar 8, 2005
2
GB
When using GROUP BY to sort the results of a select statement, is it possible to have it ignore certain common words. i.e "The", "A".

For instance, say you were sorting a list of book titles; you would want "The Promise of Happiness" to be near "Pride and Prejudice", not "Theatre Talk: Voices of Irish Theatre Practitioners".

Thanks.
 
ORDER BY
case when left(title,4) = 'The '
then substring(title from 5
for length(substring)-4)
when left(title,3) = 'An '
then substring(title from 4
for length(substring)-3)
when left(title,2) = 'A '
then substring(title from 3
for length(substring)-2)
else title
end

rudy | r937.com | Ask the Expert | Premium SQL Articles
SQL for Database-Driven Web Sites (next course starts March 6 2005)
 
glad you asked, because i just spotted the typos :)

ORDER BY
case when left(title,4) = 'The '
then substring(title from 5
for length(title)-4)
when left(title,3) = 'An '
then substring(title from 4
for length(title)-3)
when left(title,2) = 'A '
then substring(title from 3
for length(title)-2)
else title
end

rudy | r937.com | Ask the Expert | Premium SQL Articles
SQL for Database-Driven Web Sites (next course starts May 8 2005)
 
There's no LEFT in ANSI SQL :)

"left(title, 4)" -> "substring(title FROM 1 FOR 4)"

And there's no need for "for" in substring:
substring(title from 5)

;-)

Dieter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top