dbTalk Databases Forums  

Urgent : call a column in where clause which looks like this 'KS','NE','CO','OK','AR','MO','IA'

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss Urgent : call a column in where clause which looks like this 'KS','NE','CO','OK','AR','MO','IA' in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Radhika Shivaraman
 
Posts: n/a

Default Urgent : call a column in where clause which looks like this 'KS','NE','CO','OK','AR','MO','IA' - 11-18-2010 , 08:49 AM






Hi All :
I have a tableB with a column SurroundingStates.
A value in that table is like above.

Now i join the table and call this column like this

Select *
From tableA A
inner join tableB b
on A.state in (B.SurroundingStates)
and A.State = B.State
where A.State = 'KS'

what i want the query to look like is :
Select *
From tableA A
inner join tableB b
on A.state in ('KS','NE','CO','OK','AR','MO','IA')
and A.State = B.State
where A.State = 'KS'

How do i write it , as the first query is not working.
thank you

Reply With Quote
  #2  
Old   
michald
 
Posts: n/a

Default Re: Urgent : call a column in where clause which looks like this 'KS','NE','CO','OK','AR','MO','IA' - 11-19-2010 , 03:35 AM






On Nov 18, 3:49*pm, Radhika Shivaraman <rshivara... (AT) gmail (DOT) com> wrote:
Quote:
Hi All :
I have a tableB with a column SurroundingStates.
A value in that table is like above.

Now i join the table and call this column like this

Select *
From tableA A
inner join tableB b
on A.state in (B.SurroundingStates)
and A.State = B.State
where A.State = 'KS'

what i want the query to look like is :
Select *
From tableA A
inner join tableB b
on A.state in ('KS','NE','CO','OK','AR','MO','IA')
and A.State = B.State
where A.State = 'KS'

How do i write it , as the first query is not working.
thank you
Select *
From tableA A
inner join tableB b
on (
(A.state in B.SurroundingStates )
OR
(A.State = B.State )
)
where A.State = 'KS'

Reply With Quote
  #3  
Old   
Cleary
 
Posts: n/a

Default Re: Urgent : call a column in where clause which looks like this 'KS','NE','CO','OK','AR','MO','IA' - 11-19-2010 , 06:22 AM



On Nov 18, 9:49*am, Radhika Shivaraman <rshivara... (AT) gmail (DOT) com> wrote:
Quote:
Hi All :
I have a tableB with a column SurroundingStates.
A value in that table is like above.

Now i join the table and call this column like this

Select *
From tableA A
inner join tableB b
on A.state in (B.SurroundingStates)
and A.State = B.State
where A.State = 'KS'

what i want the query to look like is :
Select *
From tableA A
inner join tableB b
on A.state in ('KS','NE','CO','OK','AR','MO','IA')
and A.State = B.State
where A.State = 'KS'

How do i write it , as the first query is not working.
thank you
Try this
Select *
From tableA A
inner join tableB b
on (B.SurroundingStates LIKE '%'''+A.state '''%' -- notice the
embedded quoted quotes
OR A.State = B.State)
where A.State = 'KS'

Reply With Quote
  #4  
Old   
--CELKO--
 
Posts: n/a

Default Re: Urgent : call a column in where clause which looks like this 'KS','NE','CO','OK','AR','MO','IA' - 11-19-2010 , 07:20 AM



"A problem well stated is a problem half solved." -- Charles F.
Kettering

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. If you know how, follow ISO-11179 data element naming
conventions and formatting rules. Temporal data should use ISO-8601
formats. Code should be in Standard SQL as much as possible and not
local dialect.

Sample data is also a good idea, along with clear specifications. It
is very hard to debug code when you do not let us see it. If you want
to learn how to ask a question on a Newsgroup, look at:
http://www.catb.org/~esr/faqs/smart-questions.html

Reply With Quote
  #5  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Urgent : call a column in where clause which looks like this 'KS','NE','CO','OK','AR','MO','IA' - 11-21-2010 , 05:06 PM



Radhika Shivaraman (rshivaraman (AT) gmail (DOT) com) writes:
Quote:
Hi All :
I have a tableB with a column SurroundingStates.
A value in that table is like above.

Now i join the table and call this column like this

Select *
From tableA A
inner join tableB b
on A.state in (B.SurroundingStates)
and A.State = B.State
where A.State = 'KS'

what i want the query to look like is :
Select *
From tableA A
inner join tableB b
on A.state in ('KS','NE','CO','OK','AR','MO','IA')
and A.State = B.State
where A.State = 'KS'

How do i write it , as the first query is not working.
Looks like your database is incorrectly designed. You would be better of
having one column per surrounding state rather than a comma-separate list.

If you insist on that design, see
http://www.sommarskog.se/arrays-in-s...tml#tablelists for a solution.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
  #6  
Old   
LP
 
Posts: n/a

Default Re: Urgent : call a column in where clause which looks like this 'KS','NE','CO','OK','AR','MO','IA' - 11-24-2010 , 06:16 PM



Looks like the data model should be refactored - the list of
surrounding states should be in a table with many to many
relationship. If Oklahoma is surrounded by Ark, Missouri, and
Nebraska, it would look like so:

state related_state
---------------------------
OK AR
OK MO
OK NE

Then the query is a very simple set operation. If you absolutely
cannot change the model, at least you can use the info here:
http://www.sommarskog.se/arrays-in-s...tml#tablelists
to convert the comma-separated list of related states into a temporary
table, and use that temporary table for your query.

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.