Help using like operator

  • I have a table

    create table accounts

    (

    accname varchar(100),

    recstatus bit

    )

    and records are as follows:

    Insert into accounts values ('Pepsi Co',0)

    Insert into accounts values ('Pepsi Co',0)

    Insert into accounts values ('Prudential',1)

    Insert into accounts values ('Lennox International',1)

    Insert into accounts values ('Eurosystems (Harveynash Inc) [OLD]',0)

    Insert into accounts values ('ETJ Holdings, inc. [OLD]',0)

    Insert into accounts values ('I-nnovate [OLD]',1)

    Insert into accounts values ('Iflowsoft, LLC [OLD]',1)

    Insert into accounts values ('1st OBJECT Inc [OLD]',0)

    Insert into accounts values ('Jet Aviation Holdings, Inc [OLD]',0)

    Now i want to get the values of the table whose accname contains [old].So please help me

  • iiit.raju (4/14/2013)


    I have a table

    create table accounts

    (

    accname varchar(100),

    recstatus bit

    )

    and records are as follows:

    Insert into accounts values ('Pepsi Co',0)

    Insert into accounts values ('Pepsi Co',0)

    Insert into accounts values ('Prudential',1)

    Insert into accounts values ('Lennox International',1)

    Insert into accounts values ('Eurosystems (Harveynash Inc) [OLD]',0)

    Insert into accounts values ('ETJ Holdings, inc. [OLD]',0)

    Insert into accounts values ('I-nnovate [OLD]',1)

    Insert into accounts values ('Iflowsoft, LLC [OLD]',1)

    Insert into accounts values ('1st OBJECT Inc [OLD]',0)

    Insert into accounts values ('Jet Aviation Holdings, Inc [OLD]',0)

    Now i want to get the values of the table whose accname contains [old].So please help me

    Looking at Books Online (this isn't something I knew off the top of my head), this should work:

    Select *

    from dbo.accounts

    where accname like '%![OLD]%' escape '!';

  • This query can also be answered without using escape

    The query is shown below:

    select * from accounts where accname like '%OLD%'

    Thanks:-)

  • pankaj2.kumar (4/15/2013)


    This query can also be answered without using escape

    The query is shown below:

    select * from accounts where accname like '%OLD%'

    Except that would also match entries such as 'Jet Aviation Holdings, Inc', not just ones with OLD within the [], hence can give incorrect results.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • @thank you pettis

  • GilaMonster (4/15/2013)


    pankaj2.kumar (4/15/2013)


    This query can also be answered without using escape

    The query is shown below:

    select * from accounts where accname like '%OLD%'

    Except that would also match entries such as 'Jet Aviation Holdings, Inc', not just ones with OLD within the [], hence can give incorrect results.

    Right Gail , Pankaj query will not work correctly..

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply