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 3rd, 2004, 12:19 PM
jmlsgateway jmlsgateway is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: OKC
Posts: 342 jmlsgateway User rank is Private First Class (20 - 50 Reputation Level)jmlsgateway User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 20 h 48 m 28 sec
Reputation Power: 5
Complex INNER JOIN Tables

I have a couple tables that I am exporting data from.

I have one table (call it TABLE1) that contains abbreviations instead of actual words.

For example instead of having the full word "New York City", the column would just contain "NYC".

In another table (call it TABLE2) it has all the abbreviations and the actual words.

So in TABLE2 one column name is FieldName, another Abbreviation and another is Value.

So on a web site, to display that actual name I do something like this

Code:
SELECT CityBoxlabel AS City
FROM TABLE1 INNER JOIN 
                  TABLE 2 AS CityBox ON ISNULL(TABLE1.City, 'NYC') = CityBox.[Value]
WHERE CityBox.FieldName = 'City'


This is working great except that some of these columns contain more than one abbreviation such as "NYC,WDC"
which would stand for "New York City" and "Washington DC"
The items that have more than one abbreviation are not being pulled across because my query is looking for an "NYC,WDC" in TABLE2 to INNER JOIN on but it won't be in there. But "NYC" and "WDC" by themselves are in there.

I tried messing with the INNER JOIN statement by saying something like
TABLE2 AS CityBox ON ISNULL(TABLE1.City, 'NYC') IN CityBox.[Value] INNER JOIN

But it wasn't allowing that.
I can write a program to do this, but I would rather keep it in T-SQL.

Hope this wasn't too confusing!
Any help is greatly appreciated!!

Reply With Quote
  #2  
Old November 3rd, 2004, 02:13 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,784 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 21 h 42 m 49 sec
Reputation Power: 37
It's your database design that is the cause of your problem. You should never store multiple values in one column. Break it up into separate records.

Reply With Quote
  #3  
Old November 3rd, 2004, 02:21 PM
jmlsgateway jmlsgateway is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: OKC
Posts: 342 jmlsgateway User rank is Private First Class (20 - 50 Reputation Level)jmlsgateway User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 20 h 48 m 28 sec
Reputation Power: 5
I didn't design the database, it was already made by a different company. I am just trying to get the values out to use on a different website.

I really can't break up the record values in the main database.

Is there a way in T-SQL to do an if statement where it splits the record value at the commas and then I could just look at each one of those values?

Last edited by jmlsgateway : November 3rd, 2004 at 02:27 PM.

Reply With Quote
  #4  
Old November 3rd, 2004, 03:04 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,784 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 21 h 42 m 49 sec
Reputation Power: 37
http://www.sqlteam.com/item.asp?ItemID=2652

Reply With Quote
  #5  
Old November 3rd, 2004, 03:08 PM
Wingman Wingman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Bavaria, Germany
Posts: 140 Wingman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 40 m 41 sec
Reputation Power: 6
PATINDEX, SUBSTR(ING) or LIKE might be your friends

Code:
... TABLE 2 AS CityBox ON CityBox.[Value] LIKE '%' + ISNULL(TABLE1.City, 'NYC')  + '%'


Code:
 
.. TABLE 2 AS CityBox ON CityBox.[Value] =
-- 1. Part
	SUBSTRING(TABLE1.City, PATINDEX('%,%', TABLE1.City) + 1, LEN(TABLE1.City))
-- 2. Part (if any)
OR CityBox.[Value]
	SUBSTRING(TABLE1.City, 0, PATINDEX('%,%', TABLE1.City))
)


But that's ugly, slow and use at your own risk 8-)

Reply With Quote
  #6  
Old November 9th, 2004, 12:17 PM
jmlsgateway jmlsgateway is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: OKC
Posts: 342 jmlsgateway User rank is Private First Class (20 - 50 Reputation Level)jmlsgateway User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 20 h 48 m 28 sec
Reputation Power: 5
I tried messing with the 2nd part using the substring method, and it worked except for the fact that if there was only one value and a comma couldn't be found, then it didn't return the information for the ID. Also there could be an undertermind amount of comma seperated values, so I couldn't really use that method.

Thanks for the reply!!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Complex INNER JOIN Tables


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 4 hosted by Hostway
Stay green...Green IT