Solved

SQL WHERE comma delimited int string

Posted on 2010-11-11
6
443 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
  • 4
6 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
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:ScottPletcher
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:ScottPletcher
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:ScottPletcher
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

910 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now