Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP General (http://www.velocityreviews.com/forums/f65-asp-general.html)
-   -   Using 'Like' with 'If' statement ?? (http://www.velocityreviews.com/forums/t791534-using-like-with-if-statement.html)

David 11-11-2003 11:11 AM

Using 'Like' with 'If' statement ??
 
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

Rob Meade 11-11-2003 11:20 AM

Re: Using 'Like' with 'If' statement ??
 
"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



Bob Barrows 11-11-2003 11:37 AM

Re: Using 'Like' with 'If' statement ??
 
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"




All times are GMT. The time now is 05:13 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.