Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

All,
Require your help on this. Following values are stored in the column "description_column" in one of the tables called description_table. The values are
"My name is Robert Jr." and another value is
"Robert is my name." .

So to select "Robert Jr." from the table we issue the following query,
Select * from description_table
where 'Robert Jr.' like '%'+description+'%'

But the query returns both the values "My name is Robert Jr." and "Robert is my name.", though we only want "My name is Robert Jr." to be returned.

Is it possible in PERL to look for the exact match after the query returns both the rows, so only "Robert Jr." is matched and "Robert" is not selected.

Regards

Replies are listed 'Best First'.
Re: Exact Match
by davorg (Chancellor) on Jun 02, 2006 at 10:31 UTC

    Rather than working around the problem in Perl, your time would be better spent getting your SQL to return the correct data.

    You don't say which database system you are using, but I think you want something like:

    select * from description_table where description_column like '%Robert Jr.%';

    To be honest, I'm surprised that your current SQL even compiles. I assume it's preprocessed in some way before it's sent to the database.

    Note: Of course select * is almost always bad practice - you should replace * with the list of columns that you're interested in.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Thanks for the update.. I will try your suggestions and get back.. Regards
Re: Exact Match
by Joost (Canon) on Jun 02, 2006 at 10:28 UTC
Re: Exact Match
by McDarren (Abbot) on Jun 02, 2006 at 10:31 UTC
    erm, I think you have your SQL where clause back-to-front. Why don't you try:
    SELECT * FROM description_table WHERE description_column LIKE '%Robert Jr%';
Re: Exact Match
by marto (Cardinal) on Jun 02, 2006 at 10:30 UTC
    Anonymous Monk,

    Perhaps I am not understanding the question you trying to ask, would you not be better doing something like:
    select * from description_table where description column = 'My name is Robert Jr.'

    Update: The answer above is based purely on the 'we only want "My name is Robert Jr." to be returned.'

    Please read the PerlMonks FAQ and How do I post a question effectively? if you have not done so already.

    Martin