Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Database and Reporting > What Function to use? Query


Reply
 
Thread Tools Display Modes
  #1  
Old 06-03-2004, 11:53 AM
MikeBAM MikeBAM is offline
Newcomer
 
Join Date: Aug 2003
Posts: 12
Default What Function to use? Query


I'm trying to make a query but I don't know what function to use for one of my feilds.

I have an Options feild with data looking like this: (____ = blank or null)

Options:
_____
2
_____
2,3
4,5
_____
4,6,8
3
1,8
1,6,8
2,4,8
1,2,3,4


I need to know how to check which ones have a 2 or a 4 or both in the string.
So the data that comes back looks like:
2
2,3
4,5
4,6,8
2,4,8
1,2,3,4

I'm going to be making the criteria more complex then this though
Reply With Quote
  #2  
Old 06-03-2004, 11:59 AM
HardCode's Avatar
HardCode HardCode is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Feb 2004
Location: New Jersey
Posts: 3,338
Default

Well, if there are always single-digit items in the list (i.e. no 12 or 34), then you can use WHERE Options LIKE '*2*' OR Options LIKE '*4*'

Personally, I would set this field to one value if the list is small, using 1,2,4,8,16,32,64 ...etc so In your code you can check the single value for the bit:
Code:
If ((iOptions And 4) <> 0) Then

This will check if the bit for 4 is "on", in other words if 4 is in the list. So if iOptions = 7 then 4 is in the list (4 + 2 + 1 = 7)
__________________
DON'T CLICK HERE

Useful forum tags: [VB][/VB], [CODE][/CODE], [HTML][/HTML]
Reply With Quote
  #3  
Old 06-03-2004, 11:51 PM
Dennis DVR's Avatar
Dennis DVR Dennis DVR is offline
Back in the Game

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Manila Philippines
Posts: 3,555
Default

or you could use the In Keyword
Code:
SELECT Field1 FROM Table1 WHERE Field1 IN ('2','4')
__________________
Avatar by Lebb

[Posting Guidelines] [Standards & Practices Tutorial] [Participate here effectively]
Our knowledge can only be finite, while our ignorance must necessarily be infinite. Karl Popper
Reply With Quote
  #4  
Old 06-04-2004, 06:30 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

How big is your table? If this a generally small table, definately use IN. If this is under say 200,000 records, you are fine. However, if using a large database, IN is very expensive in terms of memory and you will see some performance issues (in terms of speed).
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #5  
Old 06-04-2004, 07:33 AM
HardCode's Avatar
HardCode HardCode is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Feb 2004
Location: New Jersey
Posts: 3,338
Default

I don't think IN will work ... it seems that he has an un-normalized table because the field OPTIONS contains a text string of numbers that are comma separated. So, IN('2','4') should fail because the value is really '1,2,3,4'.
__________________
DON'T CLICK HERE

Useful forum tags: [VB][/VB], [CODE][/CODE], [HTML][/HTML]
Reply With Quote
  #6  
Old 06-04-2004, 07:47 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Well, actually a complicated query could be created. In SQL Server I would use a CASE statment, in Access the IIF(), something like:

Code:
"Select IIF(InStr([MyTable].[thisField],"2")>0,1,0) as total_Count] From [MyTable];"

There are definately ways to parse this out . Of course from VB, there are certain things you can and can't do (especially regarding the IIF statement, some native SQL Server calls, etc.).
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #7  
Old 06-04-2004, 08:13 AM
Dennis DVR's Avatar
Dennis DVR Dennis DVR is offline
Back in the Game

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Manila Philippines
Posts: 3,555
Default

well re-reading the question the code i posted will not fit to his requirements i didn't realize that the field content is a comma seperated, but it doesn't mean he can't use the IN keyword for his requirement (with ACCESS).

and another thing if he is using ADO then your post will fail either

perhaps i shouldn't answer when i'm busy
__________________
Avatar by Lebb

[Posting Guidelines] [Standards & Practices Tutorial] [Participate here effectively]
Our knowledge can only be finite, while our ignorance must necessarily be infinite. Karl Popper

Last edited by duane; 06-04-2004 at 08:46 AM.
Reply With Quote
  #8  
Old 06-10-2004, 09:08 PM
MikeBAM MikeBAM is offline
Newcomer
 
Join Date: Aug 2003
Posts: 12
Default

Ya thanks HardCode.
I ended up having to do a long string of:
Like("*14*") OR Like("*13*") OR Like("*12*") OR Like("*8*") OR Like("*18*") OR Like("1") OR Like("1,*")

No that In() wouldn't work your right. (that was actually the first thing I tried which you told me about from my other post i think.)
Reply With Quote
  #9  
Old 06-11-2004, 06:06 AM
MKoslof's Avatar
MKoslof MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Just curious, what database are you using? Is this Access? I can probably come up with a better query...but let me know if I can use the power of SQL Server or not .
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->