Print Page | Close Window

Find if field contains number

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=4582
Printed Date: 30 March 2026 at 10:06am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Find if field contains number
Posted By: pedalcars
Subject: Find if field contains number
Date Posted: 29 July 2003 at 2:54am
Hope this makes sense...

I have a table with a field containing a collection of numbers, each separated by a controlled character (which could be anything, like a space, comma, dash, underscore, colon, etc.), so for example one record could be:

1,2,9,13,7,4

This string will be assigned to a variable, I then want to compare this to another, single number, such that if the single number is contained within the first string, the record is displayed, otherwise it is ignored, eg:

strLotsOfNumbers = 1,2,9,13,7,4

If strLotsOfNumbers (contains) rs("SingleNumber") Then (display rs("FieldName"))


However, it also has to compare the entire number between separators, so in the example above if rs("SingleNumber") = 3 it would return FALSE, ie, wouldn't match the '3' with the second character of the '13'.

If necessary I can start, end, or both, the long string with the control character, like: ,1,2,9,13,7,4, so each number is surrounded.

Thanks in anticipation

-------------
http://www.pedalcars.info/ - www.pedalcars.info

The most fun on four wheels




Replies:
Posted By: b_bonnett
Date Posted: 29 July 2003 at 3:11am

My guess would be to surround strLotsOfNumbers in the control character, set strControl = "," (or whatever), then use:

If InStr(strLotsOfNumbers, strControl & intNumberToCompare & strControl) Then...

Thats just a guess - haven't tested it but think it should work.

Blair



-------------
Webmaster, http://www.planegallery.net/ - The Plane Gallery
Greetings From Christchurch


Posted By: Flamewave
Date Posted: 29 July 2003 at 10:16am

Another way of doing it would be to split up the number list into an array and then use a for loop to compare the numbers. This method requires a bit more code then the method bonnett mentioned, but at least this way you wouldn't be dependant upon comparing strings, which can get messy if you get extra characters in there, such as if you end up with a space between commas, then the method above would not work unless you specifically checked for that.

temparray = split(strLotsOfNumbers, ",")
For loopcounter = 0 to Ubound(temparray)
    If Int(temparray(loopcounter)) = intNumberToCompare Then
        ...
        Exit For
    End If
Next



-------------
- Flamewave

They say the grass is greener on the other side, but if you really think about it, the grass is greener on both sides.


Posted By: ljamal
Date Posted: 29 July 2003 at 11:18am
Use the inStr Method it's more efficient with longer string as you will not be looping through the an unknown number of values.

-------------
L. Jamal Walton

http://www.ljamal.com/" rel="nofollow - L. Jamal Inc : Web/ Print Design and ASP Programming



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net