Solved

SQL WHERE comma delimited int string

Posted on 2010-11-11
6
448 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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.

690 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