Problem with the LIKE operator in the Select statement.

  • Hi,

    I just faced this weird issue while checking something in a

    SP code. To simplify it I have replicated the problem below.

    Why should the value 'LeftPanelSelect' be selected as part of the select stmt below? Am I missing something?

    declare @tbl table(name varchar(30))

    insert into @tbl(name) values ('l_ChildPeopleSelect')

    insert into @tbl(name) values ('l_ChildSitesSelect')

    insert into @tbl(name) values ('LeftPanelSelect')

    insert into @tbl(name) values ('l_CompanyDetailsGetByName')

    insert into @tbl(name) values ('l_CompanyDetailsGetByName')

    insert into @tbl(name) values ('l_CompanyEventsGet')

    select * from @tbl where name like 'l_%'

  • this is because _ is a wildchard character for single character

    -Vikas Bindra

  • hi,

    refer the topics

    http://www.sqlservercentral.com/Forums/Topic551602-1325-2.aspx#bm567224

    (Home » Article Discussions » Article Discussions by Author » Discuss content posted by Susantha Bathige » LIKE operator)

    ARUN SAS

  • To search for an underscore or percent sign (_ or %), specify and use an escape character. With a tilde escape, your query would be:

    select * from @tbl where name like 'l~_%' ESCAPE '~'

  • Or the classic style

    DECLARE @t TABLE (txt VARCHAR(100))

    INSERT INTO @t VALUES ('abc')

    INSERT INTO @t VALUES ('a_bc')

    SELECT * FROM @t WHERE txt LIKE 'a[_]%'

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

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