What Function to use? Query

MikeBAM
06-03-2004, 11:53 AM
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

HardCode
06-03-2004, 11:59 AM
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:

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)

Dennis DVR
06-03-2004, 11:51 PM
or you could use the In Keyword

SELECT Field1 FROM Table1 WHERE Field1 IN ('2','4')

MKoslof
06-04-2004, 06:30 AM
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).

HardCode
06-04-2004, 07:33 AM
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'.

MKoslof
06-04-2004, 07:47 AM
Well, actually a complicated query could be created. In SQL Server I would use a CASE statment, in Access the IIF(), something like:



"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.).

Dennis DVR
06-04-2004, 08:13 AM
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 :whoops:

perhaps i shouldn't answer when i'm busy ;)

MikeBAM
06-10-2004, 09:08 PM
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.)

MKoslof
06-11-2004, 06:06 AM
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 :).

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum