concern on like operator

  • Hi all,

    I have tried the following query and didn't get the proper result that i am expected.

    "select class_code from tbl_class where class_code like 'ep_f_%' "

    Output:

    -------

    AP_F_SC

    AP_F_VAL

    AP_FFFB

    AP_FFFH

    But I have expected the result as

    AP_F_SC

    AP_F_VAL

    Can anybody inform me whether I am correct or not

    Regards

    Durgesh

  • Did you try to read BOL?

    _____________
    Code for TallyGenerator

  • Don't forget that the underscore character is treated as a a single-character wildcard, not an explicit character.

  • If you want the underscores to be treated as literals rather than wildcards, you need to escape them.

    SELECT class_code FROM tbl_class WHERE class_code LIKE 'ep/_f/_%' ESCAPE '/'

    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
  • GilaMonster (6/17/2009)


    If you want the underscores to be treated as literals rather than wildcards, you need to escape them.

    SELECT class_code FROM tbl_class WHERE class_code LIKE 'ep/_f/_%' ESCAPE '/'

    You should be banned from SQL2000 forum. :laugh:

    Your scripting habits way out of it. 🙂

    I'd post compatible version but cannot access BOL now to copy-paste from there. 😉

    _____________
    Code for TallyGenerator

  • Just tried on SQL Server 2000. ESCAPE works fine 😉

    DECLARE @t TABLE (Txt VARCHAR(20))

    INSERT INTO @t

    SELECT 'AP_F_SC'

    UNION ALL SELECT 'AP_F_VAL'

    UNION ALL SELECT 'AP_FFFB'

    UNION ALL SELECT 'AP_FFFH'

    SELECT

    *

    FROM @t

    WHERE Txt LIKE 'AP/_F/_%' ESCAPE '/'

    Flo

  • Florian Reischl (6/17/2009)


    Just tried on SQL Server 2000. ESCAPE works fine 😉

    I was going to say...

    I know I've used ESCAPE on SQL 2000 before...

    http://msdn.microsoft.com/en-us/library/aa933232(SQL.80).aspx

    LIKE:

    Syntax

    match_expression [ NOT ] LIKE pattern [ ESCAPE escape_character ]

    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
  • Wow, never used it in 2000 ages. Considered it as 2k5 introduction.

    Did not even try.

    [_] always worked without any problem.

    _____________
    Code for TallyGenerator

  • SET Humor ON

    A query using "like 'ep_f_%'" shouldn't return any row starting with 'AP_'... 🙂

    SET Humor OFF



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

Viewing 9 posts - 1 through 8 (of 8 total)

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