Wrox Programmer Forums
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old April 18th, 2008, 01:38 PM
Registered User
Join Date: Apr 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Select In Statement

I have a table [tbl_Controls] that contains the Field [ControlValue] nvarchar(250) (Data Example '1,2,3') w/o the '
If I have a single nuber as (Data Example '1') w/o the ', the query works correctly

I need to filter the following, The field [ProductType_ID] Integer

select ProductType_ID,Category from tbl_ProductsCategories
Where ProductType_ID in (Select Cast (ControlValue as integer) from tbl_Controls where controlid = 17)

I would greatly appreciate anyones help

Eddie Stoner
Old April 18th, 2008, 10:27 PM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

You clearly aren't going to be able to convert "1,2,3" as an integer, so I can only assume what you are intending on is to be able to use the comma separated values as filter values like you would in an IN clause. This is tricky and usually requires constructing dynamic SQL statements which isn't the most efficient.

I would question the database design. If you need to have multiple values then why not have multiple records? You are asking the database to store a set of data in a container intended for a single piece of data. If you normalize it into multiple records then you can work with normal joins against a set.

Old April 19th, 2008, 09:59 AM
Friend of Wrox
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts

I agree... the data should be normalized to have one row per control value... it's easier to maintain and, despite the length (# of rows) such a table can get to, it will always be faster than trying to do splits on the fly. Lemme know if you need help converting the control table to a normalized table.

In the meantime, you can always do something like this... without dynamic SQL...

 SELECT pc.ProductType_ID, pc.Category
   FROM tbl_ProductsCategories pc
        tbl_Controls tc
     ON tc.ControlID = 17
    AND ','+CAST(pc.ProductType_ID AS VARCHAR(10))+',' LIKE '%,'+tc.ControlValue+',%'

Like I said, that will be horribly slow compared to a properly normalized table.

--Jeff Moden

Similar Threads
Thread Thread Starter Forum Replies Last Post
select statement ??? RinoDM SQL Server 2000 7 June 19th, 2008 08:40 AM
select Statement gregalb SQL Server 2000 3 January 15th, 2008 12:00 AM
select statement help... RinoDM SQL Server 2000 13 January 10th, 2008 08:34 PM
Select from another select statement to a repeater simsen ASP.NET 2.0 Professional 0 May 2nd, 2007 04:34 PM
Select Statement jmss66 Oracle 1 May 27th, 2004 02:31 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.