473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Weird select statement ?

I have a table with a column called Schedule. It's setup as varchar(120).
Schedule can contain several items at once like 'MO','WE','FR'.

I am trying to do a select like the following
but I can't get any results back.
I'm using MS Sql 2000.

SELECT * from tblMail
where 'MO' IN (Schedule)

Is this possible ?

Thanks.

Randy
http://members.aol.com/rsmeiner
Jul 20 '05 #1
15 2093
Your design is wrong in relational terms. Don't ever put delimited lists
into a column. You should normalize your schedules into a separate table:

CREATE TABLE MailSchedules (mailid INTEGER NOT NULL REFERENCES tblMail
(mailid) /* Assumed key for tblMail */, mailschedule CHAR(2) NOT NULL CHECK
(mailschedule IN ('MO','WE','FR')), PRIMARY KEY (mailid, mailschedule))

INSERT INTO MailSchedules (mailid, mailschedule) VALUES (123,'MO')
INSERT INTO MailSchedules (mailid, mailschedule) VALUES (123,'WE')
INSERT INTO MailSchedules (mailid, mailschedule) VALUES (456,'WE')

Now your query is much easier and more efficient:

SELECT M.*
FROM tblMail AS M
JOIN MailSchedules AS S
ON M.mailid = S.mailid AND S.mailschedule = 'MO'

Also, don't use SELECT * in production code. List the column names, that way
your code should be easier to maintain when the table structures change.

Hope this helps.

--
David Portas
SQL Server MVP
--
Jul 20 '05 #2

"RSMEINER" <rs******@aol.comcrap> wrote in message
news:20***************************@mb-m25.aol.com...
I have a table with a column called Schedule. It's setup as varchar(120).
Schedule can contain several items at once like 'MO','WE','FR'.

I am trying to do a select like the following
but I can't get any results back.
I'm using MS Sql 2000.

SELECT * from tblMail
where 'MO' IN (Schedule)

Is this possible ?

Thanks.

Randy
http://members.aol.com/rsmeiner


Here is one way:

SELECT *
FROM dbo.tblMail
WHERE Schedule LIKE '%MO%'

Or if the values are in single quotes in the table:

WHERE Schedule LIKE '%''MO''%'

The fact that you have repeating values in a single column suggests the data
model may be incorrect - you may want to review your table structure and see
if you can normalize it more fully, as it will make things easier.

Simon
Jul 20 '05 #3

"RSMEINER" <rs******@aol.comcrap> wrote in message
news:20***************************@mb-m25.aol.com...
I have a table with a column called Schedule. It's setup as varchar(120).
Schedule can contain several items at once like 'MO','WE','FR'.

I am trying to do a select like the following
but I can't get any results back.
I'm using MS Sql 2000.

SELECT * from tblMail
where 'MO' IN (Schedule)

Is this possible ?

Thanks.

Randy
http://members.aol.com/rsmeiner


Here is one way:

SELECT *
FROM dbo.tblMail
WHERE Schedule LIKE '%MO%'

Or if the values are in single quotes in the table:

WHERE Schedule LIKE '%''MO''%'

The fact that you have repeating values in a single column suggests the data
model may be incorrect - you may want to review your table structure and see
if you can normalize it more fully, as it will make things easier.

Simon
Jul 20 '05 #4
>Your design is wrong in relational terms. Don't ever put delimited lists
into a column. You should normalize your schedules into a separate table:


Well, sure. This is more of a why the hell doesn't it work question
rather then a database or table design issue. Just something I was
trying out and just wanted to make it work.

But thanks for the lesson.

Randy
http://members.aol.com/rsmeiner
Jul 20 '05 #5
>Your design is wrong in relational terms. Don't ever put delimited lists
into a column. You should normalize your schedules into a separate table:


Well, sure. This is more of a why the hell doesn't it work question
rather then a database or table design issue. Just something I was
trying out and just wanted to make it work.

But thanks for the lesson.

Randy
http://members.aol.com/rsmeiner
Jul 20 '05 #6
>Here is one way:

SELECT *
FROM dbo.tblMail
WHERE Schedule LIKE '%MO%'

Or if the values are in single quotes in the table:

WHERE Schedule LIKE '%''MO''%'

The fact that you have repeating values in a single column suggests the data
model may be incorrect - you may want to review your table structure and see
if you can normalize it more fully, as it will make things easier.

Simon


Thanks, got that LIKE to work earlier

Select * from tblMail
where charindex('MO', schedule) > 0

works also.

But I just want to know why the IN doesn't work. It's maddening.
Randy
http://members.aol.com/rsmeiner
Jul 20 '05 #7
RSMEINER wrote:
<snip>
But I just want to know why the IN doesn't work. It's maddening.

Randy
http://members.aol.com/rsmeiner


IN doesn't work, because the column schedule holds one value (it's a
scalar), even if it's value is '''MO'',''WE'',''FR'''. IN will do exact
match comparisons.

If you google "Dynamic SQL" you will get a link to Erland Sommerskog's
site which explains how you can use dynamic SQL to make this thing work.
Remember that SQL implementations use a 'simple' one pass compiler.
Using dynamic SQL you can basically create a two pass compiler. The
first pass will change

WHERE 'MO' IN ('''MO'',''WE'',''FR''')

to

WHERE 'MO' IN ('MO','WE','FR')

The second pass will be the actual query the way you intended it.

Of course, this all becomes very messy very fast, and it is therefore
not recommended. But I guess others have already mentioned that :-)

Hope this helps,
Gert-Jan

--
(Please reply only to the newsgroup)
Jul 20 '05 #8
>Here is one way:

SELECT *
FROM dbo.tblMail
WHERE Schedule LIKE '%MO%'

Or if the values are in single quotes in the table:

WHERE Schedule LIKE '%''MO''%'

The fact that you have repeating values in a single column suggests the data
model may be incorrect - you may want to review your table structure and see
if you can normalize it more fully, as it will make things easier.

Simon


Thanks, got that LIKE to work earlier

Select * from tblMail
where charindex('MO', schedule) > 0

works also.

But I just want to know why the IN doesn't work. It's maddening.
Randy
http://members.aol.com/rsmeiner
Jul 20 '05 #9
RSMEINER wrote:
<snip>
But I just want to know why the IN doesn't work. It's maddening.

Randy
http://members.aol.com/rsmeiner


IN doesn't work, because the column schedule holds one value (it's a
scalar), even if it's value is '''MO'',''WE'',''FR'''. IN will do exact
match comparisons.

If you google "Dynamic SQL" you will get a link to Erland Sommerskog's
site which explains how you can use dynamic SQL to make this thing work.
Remember that SQL implementations use a 'simple' one pass compiler.
Using dynamic SQL you can basically create a two pass compiler. The
first pass will change

WHERE 'MO' IN ('''MO'',''WE'',''FR''')

to

WHERE 'MO' IN ('MO','WE','FR')

The second pass will be the actual query the way you intended it.

Of course, this all becomes very messy very fast, and it is therefore
not recommended. But I guess others have already mentioned that :-)

Hope this helps,
Gert-Jan

--
(Please reply only to the newsgroup)
Jul 20 '05 #10
>If you google "Dynamic SQL" you will get a link to Erland Sommerskog's
site which explains how you can use dynamic SQL to make this thing work.
Remember that SQL implementations use a 'simple' one pass compiler.
Using dynamic SQL you can basically create a two pass compiler. The
first pass will change

WHERE 'MO' IN ('''MO'',''WE'',''FR''')

to

WHERE 'MO' IN ('MO','WE','FR')

The second pass will be the actual query the way you intended it.

Of course, this all becomes very messy very fast, and it is therefore
not recommended. But I guess others have already mentioned that :-)

Hope this helps,
Gert-Jan


You mean something like this ?

declare @cmd VARCHAR(1024)
declare @Schedule VARCHAR(120)
SET @cmd = ''
SET @Schedule = '''A'''
SET @schedule = @Schedule + ',' + '''B'''
SET @schedule = @Schedule + ',' + '''C'''
PRINT @Schedule

Set @cmd = 'SELECT ' + '''Y''' +
' WHERE ' + '''A''' +
' IN ('+ @Schedule + ')'
Exec (@cmd)

This works. I'm just a stubborn old fart.

Thanks.

Randy
http://members.aol.com/rsmeiner
Jul 20 '05 #11
>If you google "Dynamic SQL" you will get a link to Erland Sommerskog's
site which explains how you can use dynamic SQL to make this thing work.
Remember that SQL implementations use a 'simple' one pass compiler.
Using dynamic SQL you can basically create a two pass compiler. The
first pass will change

WHERE 'MO' IN ('''MO'',''WE'',''FR''')

to

WHERE 'MO' IN ('MO','WE','FR')

The second pass will be the actual query the way you intended it.

Of course, this all becomes very messy very fast, and it is therefore
not recommended. But I guess others have already mentioned that :-)

Hope this helps,
Gert-Jan


You mean something like this ?

declare @cmd VARCHAR(1024)
declare @Schedule VARCHAR(120)
SET @cmd = ''
SET @Schedule = '''A'''
SET @schedule = @Schedule + ',' + '''B'''
SET @schedule = @Schedule + ',' + '''C'''
PRINT @Schedule

Set @cmd = 'SELECT ' + '''Y''' +
' WHERE ' + '''A''' +
' IN ('+ @Schedule + ')'
Exec (@cmd)

This works. I'm just a stubborn old fart.

Thanks.

Randy
http://members.aol.com/rsmeiner
Jul 20 '05 #12
-P-
It doesn't work because you're confusing a string containing commas with an array whose items happen to be separated by
commas.

If you type the query out longhand, you'll see that you're passing an array of values to the IN operator
SELECT foo
FROM bar
WHERE bar.foobar IN ('a', 'b', 'c') ;

'a', 'b', 'c' is a tuple with three entries. You separate those entries with a comma in standard SQL syntax.

Now, when you referred to the column "SCHEDULE" in your query, you were passing a single string value, 14 characters in
length, which happened to contain commas in positions 4 and 9. Not the same thing at all....

--
Paul Horan
Buffalo, NY

"RSMEINER" <rs******@aol.comcrap> wrote in message news:20***************************@mb-m20.aol.com...
Here is one way:

SELECT *
FROM dbo.tblMail
WHERE Schedule LIKE '%MO%'

Or if the values are in single quotes in the table:

WHERE Schedule LIKE '%''MO''%'

The fact that you have repeating values in a single column suggests the data
model may be incorrect - you may want to review your table structure and see
if you can normalize it more fully, as it will make things easier.

Simon


Thanks, got that LIKE to work earlier

Select * from tblMail
where charindex('MO', schedule) > 0

works also.

But I just want to know why the IN doesn't work. It's maddening.
Randy
http://members.aol.com/rsmeiner

Jul 20 '05 #13
-P-
It doesn't work because you're confusing a string containing commas with an array whose items happen to be separated by
commas.

If you type the query out longhand, you'll see that you're passing an array of values to the IN operator
SELECT foo
FROM bar
WHERE bar.foobar IN ('a', 'b', 'c') ;

'a', 'b', 'c' is a tuple with three entries. You separate those entries with a comma in standard SQL syntax.

Now, when you referred to the column "SCHEDULE" in your query, you were passing a single string value, 14 characters in
length, which happened to contain commas in positions 4 and 9. Not the same thing at all....

--
Paul Horan
Buffalo, NY

"RSMEINER" <rs******@aol.comcrap> wrote in message news:20***************************@mb-m20.aol.com...
Here is one way:

SELECT *
FROM dbo.tblMail
WHERE Schedule LIKE '%MO%'

Or if the values are in single quotes in the table:

WHERE Schedule LIKE '%''MO''%'

The fact that you have repeating values in a single column suggests the data
model may be incorrect - you may want to review your table structure and see
if you can normalize it more fully, as it will make things easier.

Simon


Thanks, got that LIKE to work earlier

Select * from tblMail
where charindex('MO', schedule) > 0

works also.

But I just want to know why the IN doesn't work. It's maddening.
Randy
http://members.aol.com/rsmeiner

Jul 20 '05 #14
>It doesn't work because you're confusing a string containing commas with an
array whose items happen to be separated by
commas.

If you type the query out longhand, you'll see that you're passing an array
of values to the IN operator
SELECT foo
FROM bar
WHERE bar.foobar IN ('a', 'b', 'c') ;

'a', 'b', 'c' is a tuple with three entries. You separate those entries with
a comma in standard SQL syntax.

Now, when you referred to the column "SCHEDULE" in your query, you were
passing a single string value, 14 characters in
length, which happened to contain commas in positions 4 and 9. Not the same
thing at all....

--
Paul Horan


Some days it just doesn't pay to play with code.
This is one of those days.

Thanks.
Randy
http://members.aol.com/rsmeiner
Jul 20 '05 #15
>It doesn't work because you're confusing a string containing commas with an
array whose items happen to be separated by
commas.

If you type the query out longhand, you'll see that you're passing an array
of values to the IN operator
SELECT foo
FROM bar
WHERE bar.foobar IN ('a', 'b', 'c') ;

'a', 'b', 'c' is a tuple with three entries. You separate those entries with
a comma in standard SQL syntax.

Now, when you referred to the column "SCHEDULE" in your query, you were
passing a single string value, 14 characters in
length, which happened to contain commas in positions 4 and 9. Not the same
thing at all....

--
Paul Horan


Some days it just doesn't pay to play with code.
This is one of those days.

Thanks.
Randy
http://members.aol.com/rsmeiner
Jul 20 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ganesan Rajaraman | last post by:
In the following examples, why the first case statement works and why the second doesn't, I wonder. Could anyone shed some light on this? Thanks, Ganesh -- this is an attempt to: -- ...
7
by: Jon Combe | last post by:
I have created the following test SQL code to illustrate a real problem I have with some SQL code. CREATE TABLE JCTable ( CustomerName varchar(50) ) ALTER TABLE JCTable ADD CustomerNo int...
1
by: Keiron Waites | last post by:
Ok to understand what I'm doing you'll have to load the HTML file attached (code also below): When you select an item on the left and select the ">>" button, some alerts will come up telling you...
0
by: Ruben Antonio Macias Doporto | last post by:
I have a ASP.NET app, and something weird is happening I have a xmldocument that has more than 10 nodes, I create an instance of a XMLNode and assign one node to it from the xmldocument. One line...
7
by: dhnriverside | last post by:
Hiya Right, I'm trying to make an A-Z page for my site. It starts on A, with a row of 26 letters along the top that you can click on. Fine so far. Now I've come to pull the data in from the...
13
by: jimjim | last post by:
Hello all, I ve come across the following code fragment and I was wondering why is the copy ctr called on return (rather than just returning the string statement obj. TIA string...
4
by: Peter Afonin | last post by:
Hello, I have a weirdest issue I've ever had. I have a function that enters some data into the Oracle table and returns the sequential row number for the new record (autonumber): Private...
11
by: Petulant | last post by:
Hello~ Help Please~ I am new to Access, so am a little behind the learning curve. I have a query that I have been running for a while that has worked fine and now (with no changes) is throwing up...
6
by: =?Utf-8?B?amVmZmVyeQ==?= | last post by:
i need help with a combo box and this same code works on my first tab with a combo box. The error or problem i have is this code causes an index out of range error when i run it on my second combo...
1
antonopn
by: antonopn | last post by:
Hello there, I have just faced a really weird problem with a query in SQL SERVER 2000. I had a database with collation SQL_Latin1_General_CP1_CI_AS and converted it in a new database with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.