Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Sybase WHERE clause for "starts with" <string>

  • 18-02-2014 9:44am
    #1
    Registered Users, Registered Users 2 Posts: 5,660 ✭✭✭


    I have a very long and cumbersome set of strings in my application. I want to uniquely identify some by tagging the current count of seconds since epoch to the start of each string. Sofar sogood.

    Problem is that when i query them with LIKE, the query takes a lot of processing.

    [HTML]SELECT SomeAttribute FROM SomeTable WHERE Attribute LIKE '%SomeString%'[/HTML]

    Is there a way to do a "starts with" query in sybase? Even roundabout would be okish.


Comments

  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    I have a very long and cumbersome set of strings in my application. I want to uniquely identify some by tagging the current count of seconds since epoch to the start of each string. Sofar sogood.

    Problem is that when i query them with LIKE, the query takes a lot of processing.

    [HTML]SELECT SomeAttribute FROM SomeTable WHERE Attribute LIKE '%SomeString%'[/HTML]

    Is there a way to do a "starts with" query in sybase? Even roundabout would be okish.

    Substring?
    http://infocenter.sybase.com/archive/index.jsp?topic=/com.sybase.infocenter.dc38151.1260/html/iqref/Substr.htm


  • Registered Users, Registered Users 2 Posts: 5,660 ✭✭✭veryangryman



    Just trying to fit that into my query but its not syntax-kosher.

    SELECT SomeAttribute FROM SomeTable WHERE Attribute SUBSTRING '%someString%'
    


  • Registered Users, Registered Users 2 Posts: 11,990 ✭✭✭✭Giblet


    Interesting fact, some databases support index lookups on like matching if it's in the following format
    SELECT SomeAttribute FROM SomeTable WHERE Attribute LIKE 'SomeString%'
    

    Seems to be true for Sybase
    http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc38151.1520/html/iqrefbb/CACGCGGC.htm
    Remove the first '%' and check it out.


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    Just trying to fit that into my query but its not syntax-kosher.

    SELECT SomeAttribute FROM SomeTable WHERE Attribute SUBSTRING '%someString%'
    

    Looking at your query and your question, you say starts with, but use '%Something%' as you pattern. Shouldn't the pattern be 'Something%' if it is the beginning of the string?

    If so, your query would be something like:
    SELECT SomeAttribute FROM SomeTable WHERE SUBSTRING(SomeAttribute, 1, 9) = 'Something'
    
    where 9 is the length of the string you are looking from, and 1 is where to search from (first character).

    I don't know whether it performs better, worse or the same as the equivalent LIKE on Sybase. Test it for your usage.


  • Technology & Internet Moderators Posts: 28,831 Mod ✭✭✭✭oscarBravo


    A decent database engine should be able to query efficiently on a LIKE 'Something%' query - if there's an index on the Attribute column. A LIKE '%Something%' will always require a table scan and can't be optimised.


  • Advertisement
Advertisement