?
Solved

SQL WHERE comma delimited int string

Posted on 2010-11-11
6
Medium Priority
?
451 Views
Last Modified: 2012-08-14
I want to in one field on a row a comma-delimited list of ints to represent days of the week.  This will represent days a customer gets service.  For instance, a customer getting service Monday, Wednesday, and Friday would have 2,4,6 stored in the field.  The idea is that I could do something like this:
 
SELECT *
FROM Customer
WHERE ServiceDays IN (2,4,6)

Open in new window

Any ideas on how to do this?
0
Comment
Question by:rmariotti
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 34116318
Use bits as flags rather than separate numbers.

You can use & to check for a bit being on, and | to set the bit on.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 34116327
For example:

1=Sunday, 2=Monday, 4=Tuesday, 8=Wed, 16=Thu, 32=Fri, 64=Sat

SELECT *
FROM Customer
WHERE (ServiceDays & 2 > 0)
   AND (ServiceDays & 8 > 0)
   AND (ServiceDays & 32 > 0)
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 34116335
Values for all 7 days can be stored in a tinyint (one byte).
0
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 58

Accepted Solution

by:
cyberkiwi earned 2000 total points
ID: 34116336

CREATE function [dbo].[values2table]
(
@values varchar(max),
@separator varchar(2)
) returns @res table ([value] varchar(50))
as
begin
declare @value varchar(50)
declare @commapos int, @lastpos int
set @commapos = 0
select @lastpos = @commapos, @commapos = charindex(@separator, @values, @lastpos+1)
while @commapos > @lastpos
begin
	select @value = substring(@values, @lastpos+1, @commapos-@lastpos-1)
	insert into @res select @value where @value <> ''
	select @lastpos = @commapos, @commapos = charindex(@separator, @values, @lastpos+1)
end
select @value = substring(@values, @lastpos+1, len(@values))
insert into @res select @value where @value <> ''
return
end
GO

SELECT *
FROM Customer
WHERE ServiceDays IN (select [value] from values2table('2,4,6', ','))

Open in new window

0
 
LVL 18

Expert Comment

by:lludden
ID: 34116361
Or you could just normalize your data and put that into a separate table:

CREATE TABLE Customers (CustID int, CustName varchar(50))
CREATE TABLE ServiceDays (CustID int, ServiceDay smallint)

You can have a view or query to denormalize the data if you need it in a different format.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 34116497
If you want Customers who get service *only* on those three days, and *not* any other days, you could do this:

SELECT *
FROM Customer
WHERE (ServiceDays & 1 = 0)
   AND (ServiceDays & 2 &gt; 0)
   AND (ServiceDays & 4 = 0)
   AND (ServiceDays & 8 &gt; 0)
   AND (ServiceDays & 16 = 0)
   AND (ServiceDays & 32 &gt; 0)
   AND (ServiceDays & 64 = 0)
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question