MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 11th, 2004, 06:44 AM
Tryst Tryst is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 146 Tryst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 26 m 49 sec
Reputation Power: 6
Extracting data from a column containing multiple values spearated by a '/' for query

Hi all,

If I have a column in one table (contracts) that contains a set of values (codes that identify the fields contract code) like as follows...

Code:
Contracts table
--------------------------------
Concode - description
--------------------------------
     KIDD - Kidderminster General  
     UNIV - University Hospitals


and then another table (controls) which has a column called contracts where the data within the 'Contracts' field is set out like the following (yes that is right, each of the data in this column are the Foreign Keys which are separated by '/' in which I need to query against the contracts table ) ...

Code:
controls table
--------------------------
Code - Contracts
--------------------------
   BA - KIDD /UNIV /NWPCT


With the those tables and the columns like they are, how can I get my query to display the following information...

PHP Code:
---------------------------------------------------
Con(Controls table) - Description(contracts table)
---------------------------------------------------
             
BA          Kidderminster General
             BA          
University Hospitals
--------------------------------------------------- 


I'm guessing my first job is to somehow extract those Foreign Keys from the 'Contracts' column in the 'Control' table

Tryst
__________________
Tryst

Reply With Quote
  #2  
Old November 11th, 2004, 09:06 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,962 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 16 h 29 m 38 sec
Reputation Power: 1024
this design violates first normal form (do a search to find out why)

your best option at this point is to redesign the tables

if you cannot, you should at least hunt down the people that designed these tables and shoot them all, slowly

you will not easily be able to do the join with sql

you may be forced to read an entire table into memory, perform an "explode/split/unstring" operation to pull out the individual code values, run additional queries to pull in the code descriptions, and do the matching with code logic in memory

or, you can try -- and it won't be easy -- to "explode/split/unstring" the code values into a temp table, which is more conducive to joining

but if you write the query to do this, then why make it a temp table, why not go back and redesign them permanently
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon

Reply With Quote
  #3  
Old November 11th, 2004, 10:04 AM
Tryst Tryst is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 146 Tryst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 26 m 49 sec
Reputation Power: 6
Quote:
Originally Posted by r937
this design violates first normal form (do a search to find out why)


Yep, I know the reasons why. It was the first thing I said to my boss when I noticed it

Quote:
Originally Posted by r937
if you cannot, you should at least hunt down the people that designed these tables and shoot them all, slowly


Would love to

Quote:
Originally Posted by r937
ior, you can try -- and it won't be easy -- to "explode/split/unstring" the code values into a temp table, which is more conducive to joining


When you say this, do you mean as in a programming language?

Quote:
Originally Posted by R937
but if you write the query to do this, then why make it a temp table, why not go back and redesign them permanently


Could do, but time is a factor.

I was going to implement (and modify) the follow so that I could perform a loop on each extraction of the foreign key and make a join, then when at the end of the field, just do to the next column. This is all nice and easy in a programming language like PHP and ASP.NET's C#, but bot sure if its at all possible in SQL :-S

Code:
CASE WHEN LEN(Contracts) - 
   LEN(REPLACE(Contracts, '/', '')) > 0 
   THEN LTRIM(SUBSTRING(Contracts, CHARINDEX('/', Contracts) + 1, CHARINDEX('/', Contracts + '/', CHARINDEX('/', Contracts) + 1) - CHARINDEX('/', Contracts) - 1))
ELSE NULL END)


Tryst

Reply With Quote
  #4  
Old November 11th, 2004, 10:27 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,962 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 16 h 29 m 38 sec
Reputation Power: 1024
does the contracts field contain spaces in addition to the slashes?

Reply With Quote
  #5  
Old November 11th, 2004, 10:43 AM
Tryst Tryst is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 146 Tryst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 26 m 49 sec
Reputation Power: 6
Quote:
Originally Posted by r937
does the contracts field contain spaces in addition to the slashes?


Yes. There could be several spaces between each data value.

Tryst

Reply With Quote
  #6  
Old November 11th, 2004, 01:08 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,962 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 16 h 29 m 38 sec
Reputation Power: 1024
this works but it will never utilize an index --
Code:
select controls.Code
     , Contracts.description
  from controls
left outer
  join Contracts
    on '/'+replace(controls.Contracts,' ','')+'/'
  like '%/'+Contracts.Concode+'/%'     

Reply With Quote
  #7  
Old November 12th, 2004, 02:54 AM
Tryst Tryst is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 146 Tryst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 26 m 49 sec
Reputation Power: 6
Thats awseome stuff.

There is one little problem though, The query is only returning 14 rows and not 15 (as there are 15 entries in total in the Controls.Contracts column). I have included a screenshot of the table. I think the problem lies in the last two entries in the second row.

Also, another question. How does this query work when on the 'ON' clause you have specified that an entry in the controls.contracts column must begin with a '/' and end with a '/' to mamke a JOIN (taking into consideration the first and last elements in the columns) - or am I understanding the query wrong

Code:
ON '/' + REPLACE(controls.contracts, ' ', '') + '/'


Thanks

Tryst
Attached Images
File Type: gif controls_table.gif (10.6 KB, 183 views)

Reply With Quote
  #8  
Old November 12th, 2004, 06:30 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,962 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 16 h 29 m 38 sec
Reputation Power: 1024
the missing entry is likely due to the data being missing

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Extracting data from columns that contain multiple values spearated by a comma


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT