473,394 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

SQL IN Query

@sh
A very simple one I hope?

I have a table like this...

ListID PropertyID MessageBody
1 75,62,2,4 erter tdfgs fd
2 6,25,75,23 dsfgsdfgsdfg sd
3 2,7,36,2 dfsdfgs dfgsdf
4 4,73,75,4 s dfgsdfg sfdg

I want to select a recordset containing only properties with a PropertyID of
75, I've tried this...

"SELECT * From Tbl_ListIDs WHERE " & Request("PropertyID") & " IN
PropertyID"

But I'm getting a SQL error of incorrect syntax? I appreciate that normally
the conditions of the IN statement are visa versa but is there an easier way
to achieve what I'm trying to do above?

Cheers, @sh
May 19 '06 #1
7 1413
select * from tbl_listids where properid like '%75%'

May 19 '06 #2
SQL
what about 2
you will get back the second row with 25 and 23

create table Tbl_ListIDs (ListID int,PropertyID varchar(49))
insert into Tbl_ListIDs
select 1,'75,62,2,4' union all
select 2,'6,25,75,23' union all
select 3,'2,7,36,2' union all
select 4,'4,73,75,4' union all
select 5,'75,62,1,42'

--This should return only 2's that are not part of a bigger number
select * from tbl_listids where PropertyID like '%,2,%' OR
PropertyID like '2,%'
or PropertyID like '%,2'

Denis the SQL Menace
http://sqlservercode.blogspot.com/

May 19 '06 #3
@sh (sp**@spam.com) writes:
ListID PropertyID MessageBody
1 75,62,2,4 erter tdfgs fd
2 6,25,75,23 dsfgsdfgsdfg sd
3 2,7,36,2 dfsdfgs dfgsdf
4 4,73,75,4 s dfgsdfg sfdg

I want to select a recordset containing only properties with a
PropertyID of 75, I've tried this...

"SELECT * From Tbl_ListIDs WHERE " & Request("PropertyID") & " IN
PropertyID"
First of all: do never include user-input data directly an SQL string
like that! That's opens for a security risk known as SQL injection.
Always used parameterised commands. This is also good for performance.

And don't use SELECT * in production code!
But I'm getting a SQL error of incorrect syntax? I appreciate that
normally the conditions of the IN statement are visa versa but is there
an easier way to achieve what I'm trying to do above?


There is no easy way, because this is a database design that violates the
first normal form by having a repeating group.

If you have control over the data model, I strongly recommend you change
the table definition, so that the property ids are strored in a subtable:

CREATE TABLE listproperties (
ListID int NOT NULL,
PropertyID int NOT NULL,
CONSTRAINT pk_listprops PRIMARY KEY (ListID, PropertyID))

If you do not have control over the data model, this may be your best
bet:

SELECT ListID, MessageBody
FROM Tbl_ListIDs
WHERE ',' + ltrim(str(@propertyid)) + ',' LIKE ',' + PropertyID + ','

It is not going to perform very well.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
May 19 '06 #4
On 19 May 2006 05:44:05 -0700, SQL wrote:
--This should return only 2's that are not part of a bigger number
select * from tbl_listids where PropertyID like '%,2,%' OR
PropertyID like '2,%'
or PropertyID like '%,2'


Hi Denis,

Or (shorter)

SELECT something
FROM listids
WHERE ',' + PropertyID + ',' LIKE '%,2,%'

--
Hugo Kornelis, SQL Server MVP
May 19 '06 #5
Please read any book on RDBMS and learn what First Normal Form is
(1NF). You think that SQL is a MV database or perhaps an old COBOL file
with variant records. That is fundamentally wrong. Do you understand
what a scalar value is? How about an atomic value (slight difference)?
Normalize the schema, which is the real problem, and your question is
answered immediately by the data.

May 19 '06 #6
@sh
Many thanks for all the help posted in response, I think I'll take the
suggestion below although do take into account all points raised, especially
that regarding SQL Injection - this application isn't a public one, its
within a secure administration panel, therefore the user would first have to
get beyond the security aspect before they could even attempt such an
exploit.

Also, all users are actually staff and so the risks are minimal.

Thanks once again!

Cheers, Ash

"Hugo Kornelis" <hu**@perFact.REMOVETHIS.info.INVALID> wrote in message
news:5n********************************@4ax.com...
On 19 May 2006 05:44:05 -0700, SQL wrote:
--This should return only 2's that are not part of a bigger number
select * from tbl_listids where PropertyID like '%,2,%' OR
PropertyID like '2,%'
or PropertyID like '%,2'


Hi Denis,

Or (shorter)

SELECT something
FROM listids
WHERE ',' + PropertyID + ',' LIKE '%,2,%'

--
Hugo Kornelis, SQL Server MVP

May 22 '06 #7
On 19 May 2006 15:58:35 -0700, "--CELKO--" <jc*******@earthlink.net>
wrote:
Please read any book on RDBMS and learn what First Normal Form is
(1NF). You think that SQL is a MV database or perhaps an old COBOL file
with variant records. That is fundamentally wrong. Do you understand
what a scalar value is? How about an atomic value (slight difference)?
Normalize the schema, which is the real problem, and your question is
answered immediately by the data.

Excellent advise, however not realistic if the data model is not under
the posters control, right?
Yeah, not very normalized.. indeed!
May 23 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
29
by: shank | last post by:
1) I'm getting this error: Syntax error (missing operator) in query expression on the below statement. Can I get some advice. 2) I searched ASPFAQ and came up blank. Where can find the "rules"...
9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
3
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
4
by: Diamondback | last post by:
I have two tables, WIDGETS and VERSIONS. The WIDGETS table has descriptive information about the widgets while the VERSIONS table contains IDs relating to different iterations of those widgets...
14
by: Dave Thomas | last post by:
If I have a table set up like this: Name | VARCHAR Email | VARCHAR Age | TINYINT | NULL (Default: NULL) And I want the user to enter his or her name, email, and age - but AGE is optional. ...
0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
4
by: Stan | last post by:
I am using MS Office Access 2003 (11.5614). My basic question is can I run a query of a query datasheet. I want to use more that one criteria and can not get that query to work. I thought I...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.