Using 'Like' with 'If' statement ??

Discussion in 'ASP General' started by David, Nov 11, 2003.

  1. David

    David Guest

    Hi,

    Quick question.

    If I have a recordset value in RS("ProductName"), is it possible to
    test for a part string in this value ?

    I would be looking for all products starting 'Blue' and with misc
    endings.
    So I will require the search string Blue.*

    Can I do something like:


    If RS("ProductName") = like(Blue.*) then.....

    or

    can this only be done within the select statement ?


    Thanks


    David
     
    David, Nov 11, 2003
    #1
    1. Advertising

  2. David

    Rob Meade Guest

    "David" wrote ...

    > If I have a recordset value in RS("ProductName"), is it possible to
    > test for a part string in this value ?


    yes

    > can this only be done within the select statement ?


    If you want to do this within the ASP and not the select statement, you can
    use InStr.

    InStr will return a numeric value for the character location of the first
    found match.

    So -

    strText = "blue.pants"

    Location = InStr(strText, "blue")

    In this case, InStr would return as 1 - the first character in the string
    etc...

    You would need to put this in the loop for your recordset.

    Do While Not RS.BOF And Not RS.EOF

    strText = RS("text")

    Location = InStr(strText, "blue")

    If InStr > 0 Then
    ' do something
    End If

    RS.MoveNext
    Loop


    You might also want to put LCase() around strText within the InStr line
    above - then you can be sure it wont be case sensitive etc when matching...

    To be honest, depending on your needs, it might be a lot quicker to just
    change your SQL statement to return all the records you want where it
    already contains "blue" in the relevant field etc...

    Hope this helps,

    Regards

    Rob
     
    Rob Meade, Nov 11, 2003
    #2
    1. Advertising

  3. David

    Bob Barrows Guest

    David wrote:
    > Hi,
    >
    > Quick question.
    >
    > If I have a recordset value in RS("ProductName"), is it possible to
    > test for a part string in this value ?
    >
    > I would be looking for all products starting 'Blue' and with misc
    > endings.
    > So I will require the search string Blue.*
    >
    > Can I do something like:
    >
    >
    > If RS("ProductName") = like(Blue.*) then.....
    >
    > or
    >
    > can this only be done within the select statement ?
    >
    >
    > Thanks
    >
    >
    > David


    In addition to Robb's suggestion (InStr), you can use the recordset's Filter
    property and the recordset's Find method to perform a Like comparison. Check
    them out in online help (go to msdn.microsoft.com/library if you have no
    online help installed on your pc. The ADO reference is in the Data Access
    node)

    However, you would be much better off incorporating this filter in the SQL
    statement so that you will not be retrieving unneeded records across the
    network. Be conservative of your network's resources. In addition, you can
    perform more powerful comparisons in SQL than you can in either Filter or
    Find.

    HTH,
    Bob Barrows

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
     
    Bob Barrows, Nov 11, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    807
  2. Jay McGavren
    Replies:
    11
    Views:
    1,143
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,094
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    470
    Duncan Booth
    Jul 22, 2004
  5. Patrick Kowalzick
    Replies:
    5
    Views:
    477
    Patrick Kowalzick
    Mar 14, 2006
Loading...

Share This Page