Print Page | Close Window

From a String to an Integer

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=2774
Printed Date: 29 March 2026 at 7:40pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: From a String to an Integer
Posted By: themadjuggler
Subject: From a String to an Integer
Date Posted: 16 May 2003 at 5:16pm

Hi there,

Begin here for the long story.

     While I use databases, much of the content of my site is stored in text files (named 1.txt, 2.txt, 3.txt etc.). These numbers correspond to the primary key of one of my Acess database tables which serves as a sort of index for these.

     Anyway, I'm making a search engine for the site, and while I can identify which text files include the search string, I can't make the correlation between the text file and the appropriate record in my database.

     I have been able to take the text file name and shorten it to simply the number, leaving me with a string that contains a number in it.

Begin here for the short story

     I need to SELECT a number of fields from a table in records WHERE the primary key is equal to a specific number. The problem is, this number is contained in a string, not an integer. How should I go about changing its data type, considering I'm using ASP and VBScript?

What I've tried

      I tried to be tricky and convert each digit of the number (first checking to see how many digits it was) with the asc() command, then subtracting 48 from that result (the number 0-9 in ASCII being 48-57). Still, this yields the data type mismatch error. Should I just switch languages for this page? Anyway, here's some convoluted code:

'Determine the number of digits in the ListID

'Then convert to ASCII, adjust to actual number... JESUS CHRIST!

if len(fileNumString) = 1 then

fileNumNumber = asc(fileNumString) - 48

end if

if len(fileNumString) = 2 then

tempIntOnes = Right(fileNumString, 1)

tempIntOnes = asc(tempIntOnes) - 48

tempIntTens = asc(fileNumString) - 48

fileNumNumber = (tempIntTens * 10) + tempIntOnes

end if

if len(fileNumString) = 3 then

tempIntOnes = Right(fileNumString, 1)

tempIntOnes = asc(tempIntOnes) - 48

tempIntTens = Mid(fileNumString, 1, 1)

tempIntTens = asc(tempIntTens) - 48

tempIntHuns = asc(fileNumString) - 48

fileNumNumber = (tempIntHuns * 100) + (tempIntTens * 10) + tempIntOnes

end if

sSQL = "SELECT ListID, ListAuthor, ListTitle, ListDescription FROM Playlists WHERE ListID = '" & fileNumNumber & "'"

 

Thanks for any help you can give :-)




Replies:
Posted By: michael
Date Posted: 16 May 2003 at 6:14pm
If I understand you right you are going through way too much trouble. You can generally use something like CInt(string)
thus in the databse loose the single quotes so it should not matter at all: sSQL = "SELECT ListID, ListAuthor, ListTitle, ListDescription FROM Playlists WHERE ListID = " & string   'String being a number

-------------
http://baumannphoto.com" rel="nofollow - Blog | http://mpgtracker.com" rel="nofollow - MPG Tracker


Posted By: themadjuggler
Date Posted: 16 May 2003 at 6:25pm

That worked with my first try at the cInt function. A million thanks, this has really been irking me!

(heh, and I thought my plan was so devious :)



Posted By: ljamal
Date Posted: 16 May 2003 at 11:49pm
If you are working with large number ooutside the range of the integer datatype you may wish to use CLng rather than CInt. I think the max for integer datatype is around 32,000.



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