ASP Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreASP Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Dell PowerEdge Servers
  #1  
Old August 20th, 2003, 11:15 AM
kellylei kellylei is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 15 kellylei User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Cool String with commas, how to use instr and mid function in a loop?

Hi,

I have a string --> 7,8,9,10. This string may contain more or less than 4 numbers.
I want to separate each number and run an sql query for each number.

for example:

select * from table1 where code = '7'

select * from table1 where code ='8'

and etc....

Can I do this in a loop? Any suggestions will be appreciated!!

Thanks

Reply With Quote
  #2  
Old August 20th, 2003, 11:30 AM
Doug G Doug G is offline
Grumpier Old Moderator
Dev Shed God 12th Plane (10500 - 10999 posts)
 
Join Date: Jun 2003
Posts: 10,550 Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level)Doug G User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 4 Weeks 15 h 43 m 35 sec
Reputation Power: 640
Something like this should work (untested!)
Code:
dim n1, n2, s1
n1 = 1
do
  n2 = instr(mystr, n1)
  if n2 < n1 then exit do  'no more pieces
  s1 = mid(mystr, n1, n2)  'you may need to adjust +- 1 to account for the commas
  'do your sql stuff here, s1 will hold the parsed value
  '
loop

Reply With Quote
  #3  
Old August 20th, 2003, 11:43 AM
kellylei kellylei is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 15 kellylei User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi Doug,

umm, I'm a little lost here... I couldn't get the loop to run.

the n2 value is equal to 0.

Last edited by kellylei : August 20th, 2003 at 11:46 AM.

Reply With Quote
  #4  
Old August 20th, 2003, 12:11 PM
Vlince Vlince is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Canada, Quebec, Montreal
Posts: 410 Vlince User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Why not use the IN keyword?

Just a thought!


<%
Dim str
Dim strSql

str = "2,3,4,5"

strSql = "SELECT * " & _
"FROM Table1 " & _
"WHERE code IN (" & str & ")
'FOR DEBUG ONLY
'Response.Write strSql & "<hr>"
'Response.End
%>

Hope this helps!
Sincerely

Vlince

Reply With Quote
  #5  
Old August 20th, 2003, 12:29 PM
kellylei kellylei is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 15 kellylei User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hi Vlince,

Thanks but I need to know exactly which code being selected. I need to compare the code in two tables.

If code 2 exist in Table1, I will not insert anything into Table1. If code 2 does not exist I will execute the insert statement.

Thanks for sharing your thoughts! Let me know if you have better idea.

Reply With Quote
  #6  
Old August 20th, 2003, 12:43 PM
Vlince Vlince is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Canada, Quebec, Montreal
Posts: 410 Vlince User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
AHHHHHHHHHHH...............ok

You know Kelly these informations would've been nice to know in your *first* post...

In that case, look at what's inside the attachment file...

Hope this helps!
Sincerely

Vlince
Attached Files
File Type: asp tmp2.asp (1.1 KB, 227 views)

Reply With Quote
  #7  
Old August 20th, 2003, 02:22 PM
kellylei kellylei is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 15 kellylei User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sorry, I tried to simplify my question so it won't be too difficult to understand.

I have tried the attached coding, but I got this error message.

Object required: ''

for line ---> Set objRst = objConn.Execute(strSql)

Is there something I should change to test this script?

Reply With Quote
  #8  
Old August 20th, 2003, 02:29 PM
Vlince Vlince is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Canada, Quebec, Montreal
Posts: 410 Vlince User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Well the attachment file was to give you an idea of what to do NOT the exact working thing!

Did you create a connection object ?

In the example, I called it objConn

I even took the time to say/write:

'-----------------------------------------------------
'Open the connection object BEFORE entering the LOOP
'-----------------------------------------------------


So did you? I mean create the connection object?


Vlince

Reply With Quote
  #9  
Old August 20th, 2003, 02:39 PM
kellylei kellylei is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 15 kellylei User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
It works now!
I accidentally moved the line
set rs = Server.CreateObject("ADODB.Recordset") before the for loop.

Thank you so much! XOXO for you

I have never thought about the Split function. Thanks again.

Last edited by kellylei : August 20th, 2003 at 02:46 PM.

Reply With Quote
  #10  
Old August 20th, 2003, 02:43 PM
Vlince Vlince is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Canada, Quebec, Montreal
Posts: 410 Vlince User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Wait a minute...
I never showed/told you to EXPLICITly declare a recordset object?

You're creating too much overhead inside the Loop each time you do:

Set rs = Server.CreateObject("ADODB.Recordset")

That's why I simply created an IMPLICIT recordset to avoid the overhead!

Humm...but if it works for you then I'm happy!


Oh and...can I share them(XOXO) with others?

Hope this helps!
Sincerely

Vlince

Last edited by Vlince : August 20th, 2003 at 02:48 PM.

Reply With Quote
  #11  
Old August 20th, 2003, 03:05 PM
zimm zimm is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 30 zimm User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Dang, Vlince stole my thunder. "Split" is one of the most useful (and easy to use as well) string functions in the whole VB library.

Beats the socks off of INSTR in most cases.

Reply With Quote
  #12  
Old August 20th, 2003, 03:18 PM
unatratnag unatratnag is offline
Average Intelligence
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2003
Location: Ohio/Chicago
Posts: 678 unatratnag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 54 sec
Reputation Power: 6
Send a message via AIM to unatratnag
man, foreign guys get ALL the chicks, how can i compete? It's impossible...

Reply With Quote
  #13  
Old August 20th, 2003, 03:37 PM
Vlince Vlince is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Canada, Quebec, Montreal
Posts: 410 Vlince User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
ROTFLMAO...
Was drinking water and choked...got water coming out of my nose thanks to you...It hurts LOL

foreign guys! LOL good one

It's my "little more white skin color" from all the snow that works like a charm on girls...and my french of course!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreASP Programming > String with commas, how to use instr and mid function in a loop?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |