Solved

SQL WHERE comma delimited int string

Posted on 2010-11-11
6
442 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 110
SQL Restore Script - Syntax Error 8 70
Report Builder 9 25
SQL Date Retrival 7 26
Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

708 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

15 Experts available now in Live!

Get 1:1 Help Now