Like pattern

  • hi

    i am unable to retrieve proper data using like pattern

    ex:

    create proc OrgData

    (@OName varchar(100)

    )

    as

    begin

    select orgId,OrgName,Ordloc from OrgType where OrgName like '%' + @OName +'%'

    end

    it was executed properly. and output showing was wrong

    when we gave single character as input parameter,it shows proper results. when passing more char values it was showing wrong answers or null.

    can any one tell me the sol.

    Thanks

    Rock

  • In order to tell you what is happening, you'd have to include some sample data and results to demonstrate what you mean by wrong. To be honest I doubt very much that you are getting "wrong" results, just not the results you expect. SQL Server is pretty good at returning exactly what you have asked for.

  • I'm gonna hazard a guess here based on the normal thought process that leads to this question and guess that you're saying:

    OName = 'C'

    works fine, but

    OName = 'C,D,E,F'

    does not work.

    Is that accurate? If so, you can't provide a list in this fashion. Doing that makes SQL look for a value that literally contains the string 'C,D,E,F', not each individual value.

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • I'm still not sure what result do you want to obtain...howhver try to use '[C-F]' / [CDEF] instead of 'C, D, E, F'.

    See also BOL ---> "Pattern Matching in Search Conditions"

  • dmoldovan (10/29/2009)


    I'm still not sure what result do you want to obtain...howhver try to use '[C-F]' / [CDEF] instead of 'C, D, E, F'.

    See also BOL ---> "Pattern Matching in Search Conditions"

    He can use PATINDEX with pattern matching as you mentioned, but this will also require dynamic SQL.

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • Seth, I did not (yet) mention PATINDEX. I'd better include the link:

    http://msdn.microsoft.com/en-us/library/ms187489(SQL.90).aspx

    than just mention the topic title...sorry...

  • dmoldovan (10/29/2009)


    Seth, I did not (yet) mention PATINDEX. I'd better include the link:

    http://msdn.microsoft.com/en-us/library/ms187489(SQL.90).aspx

    than just mention the topic title...sorry...

    Heh, no, it was my fault. It's how I was considering doing it when I read it yesterday and I jumped to conclusions. For some reason I as soon as I think pattern matching my brain automatically switches to patindex.

    Also, I just rechecked and I must have been typing something wrong, dynamic SQL is not required. So... basically, disregard my previous post entirely :Wow:! Note to self, coffee, then post.

    Seth Phelabaum


    Consistency is only a virtue if you're not a screwup. 😉

    Links: How to Post Sample Data[/url] :: Running Totals[/url] :: Tally Table[/url] :: Cross Tabs/Pivots[/url] :: String Concatenation[/url]

  • Seth, never mind, the tite is confusing anyway...

    Here is something else that I think that it should be mentioned here:

    http://www.databasejournal.com/features/mssql/article.php/3827091/T-SQL-Best-Practices.htm

    - scroll down to the "Speed Up Your Searches by Prefixing Wild Card References" section...

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

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