Unix Technical Forum

Stripping the from beginning of field name when it occurs

This is a discussion on Stripping the from beginning of field name when it occurs within the MySQL forums, part of the Database Server Software category; --> I have a table storing artist_names in text format linked to other tables by a numeric auto_increment field. The ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 10:30 AM
Jonathan
 
Posts: n/a
Default Stripping the from beginning of field name when it occurs

I have a table storing artist_names in text format linked to other
tables by a numeric auto_increment field. The artist_name field is a
varchar field.

The artist_name field holds the artist name sometimes including 'the' or
'a' at the beginning of the field. Now I would like to sort them
using the artist name, ignoring the 'a' and 'the' at the start is there
some clever way to do this?

I would like for instance that 'The Smiths' would be listed under S
instead of under T when I issue a simple select key. Do I have to make
some clever join?

Thanks in advance,

Jonathan
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 10:30 AM
strawberry
 
Posts: n/a
Default Re: Stripping the from beginning of field name when it occurs

On Nov 25, 7:28 pm, Jonathan <jonat...@heelal.nl> wrote:
> I have a table storing artist_names in text format linked to other
> tables by a numeric auto_increment field. The artist_name field is a
> varchar field.
>
> The artist_name field holds the artist name sometimes including 'the' or
> 'a' at the beginning of the field. Now I would like to sort them
> using the artist name, ignoring the 'a' and 'the' at the start is there
> some clever way to do this?
>
> I would like for instance that 'The Smiths' would be listed under S
> instead of under T when I issue a simple select key. Do I have to make
> some clever join?
>
> Thanks in advance,
>
> Jonathan


Here's one way:

SELECT * FROM my_albums ORDER BY REPLACE(REPLACE(`artist`,'The
',''),'A ','');
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 10:30 AM
Paul Lautman
 
Posts: n/a
Default Re: Stripping the from beginning of field name when it occurs

Jonathan wrote:
> I have a table storing artist_names in text format linked to other
> tables by a numeric auto_increment field. The artist_name field is a
> varchar field.
>
> The artist_name field holds the artist name sometimes including 'the'
> or 'a' at the beginning of the field. Now I would like to sort
> them using the artist name, ignoring the 'a' and 'the' at the start is
> there some clever way to do this?
>
> I would like for instance that 'The Smiths' would be listed under S
> instead of under T when I issue a simple select key. Do I have to make
> some clever join?
>
> Thanks in advance,
>
> Jonathan


Here is one way

SELECT
artist_name
FROM the_table
ORDER BY
CASE
WHEN SUBSTRING_INDEX(artist_name, ' ',1) = 'The' THEN
SUBSTRING(artist_name,5)
WHEN SUBSTRING_INDEX(artist_name, ' ',1) = 'a' THEN
SUBSTRING(artist_name,3)
ELSE artist_name
END

This assumes that the "the" or "a" is followed by only 1 space.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 10:30 AM
Paul Lautman
 
Posts: n/a
Default Re: Stripping the from beginning of field name when it occurs

strawberry wrote:
> On Nov 25, 7:28 pm, Jonathan <jonat...@heelal.nl> wrote:
>> I have a table storing artist_names in text format linked to other
>> tables by a numeric auto_increment field. The artist_name field is a
>> varchar field.
>>
>> The artist_name field holds the artist name sometimes including
>> 'the' or 'a' at the beginning of the field. Now I would like
>> to sort them
>> using the artist name, ignoring the 'a' and 'the' at the start is
>> there some clever way to do this?
>>
>> I would like for instance that 'The Smiths' would be listed under S
>> instead of under T when I issue a simple select key. Do I have to
>> make some clever join?
>>
>> Thanks in advance,
>>
>> Jonathan

>
> Here's one way:
>
> SELECT * FROM my_albums ORDER BY REPLACE(REPLACE(`artist`,'The
> ',''),'A ','');


Wouldn't that make
a band called "Spank The Monkey" become "Spank Monkey"?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 10:30 AM
strawberry
 
Posts: n/a
Default Re: Stripping the from beginning of field name when it occurs

On Nov 25, 10:38 pm, "Paul Lautman" <paul.laut...@btinternet.com>
wrote:
> strawberry wrote:
> > On Nov 25, 7:28 pm, Jonathan <jonat...@heelal.nl> wrote:
> >> I have a table storing artist_names in text format linked to other
> >> tables by a numeric auto_increment field. The artist_name field is a
> >> varchar field.

>
> >> The artist_name field holds the artist name sometimes including
> >> 'the' or 'a' at the beginning of the field. Now I would like
> >> to sort them
> >> using the artist name, ignoring the 'a' and 'the' at the start is
> >> there some clever way to do this?

>
> >> I would like for instance that 'The Smiths' would be listed under S
> >> instead of under T when I issue a simple select key. Do I have to
> >> make some clever join?

>
> >> Thanks in advance,

>
> >> Jonathan

>
> > Here's one way:

>
> > SELECT * FROM my_albums ORDER BY REPLACE(REPLACE(`artist`,'The
> > ',''),'A ','');

>
> Wouldn't that make
> a band called "Spank The Monkey" become "Spank Monkey"?


It only effects order so I don't think it would make much difference.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 10:30 AM
strawberry
 
Posts: n/a
Default Re: Stripping the from beginning of field name when it occurs

On Nov 25, 10:58 pm, strawberry <zac.ca...@gmail.com> wrote:
> On Nov 25, 10:38 pm, "Paul Lautman" <paul.laut...@btinternet.com>
> wrote:
>
>
>
> > strawberry wrote:
> > > On Nov 25, 7:28 pm, Jonathan <jonat...@heelal.nl> wrote:
> > >> I have a table storing artist_names in text format linked to other
> > >> tables by a numeric auto_increment field. The artist_name field is a
> > >> varchar field.

>
> > >> The artist_name field holds the artist name sometimes including
> > >> 'the' or 'a' at the beginning of the field. Now I would like
> > >> to sort them
> > >> using the artist name, ignoring the 'a' and 'the' at the start is
> > >> there some clever way to do this?

>
> > >> I would like for instance that 'The Smiths' would be listed under S
> > >> instead of under T when I issue a simple select key. Do I have to
> > >> make some clever join?

>
> > >> Thanks in advance,

>
> > >> Jonathan

>
> > > Here's one way:

>
> > > SELECT * FROM my_albums ORDER BY REPLACE(REPLACE(`artist`,'The
> > > ',''),'A ','');

>
> > Wouldn't that make
> > a band called "Spank The Monkey" become "Spank Monkey"?

>
> It only effects order so I don't think it would make much difference.


But then again, your post is a more elegant and correct solution. In
fact, it reminds me of another...

http://www.arraystudio.com/as-worksh...-the-a-an.html
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 10:30 AM
Jonathan
 
Posts: n/a
Default Re: Stripping the from beginning of field name when it occurs

strawberry wrote:
> But then again, your post is a more elegant and correct solution. In
> fact, it reminds me of another...
>
> http://www.arraystudio.com/as-worksh...-the-a-an.html


Hi Strawberry, that is sweet ;-)

Thanks for the link.

Jonathan
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:49 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com