November 16, 2005 at 3:22 pm
HELP! HELP! HELP! I have 3 tables I am trying to select values from and inner join into one results page. One of the tables does NOT have a foreign key field to join the two tables, but instead, the value is within a field containing a string of numbers (in a comma delimited format i.e. ,1,2,3,4,5,). The value in between the commas is the same value as an ID value in a field of another table. I need to be able to select only the ID values that match the values I pull from the charindex of the other table's string field. This means only returning the correct value in between the commas that matches with that ID field value. Does that make sense? I've created a query - it doesn't err but it doesn't return any results either. Any suggestions would be greatly appreciated.
SELECT DISTINCT
D.DeptCode,D.DeptName,A.ID as AccessID,A.AccessGroup,A.AccessLevel, F.Group,F.SubGroup,F.DefaultVals
FROM tblD as D
INNER JOIN tblF ON D.DeptCode = F.DeptCode
INNER JOIN tblA ON A.ID in (Select Charindex('," & A.ID & ", ', F.DefaultVals) from tblF where active='1')
WHERE D.Active='1'
AND (F.Active='1')
AND (A.Active='1')
and (A.AccessGroup = 'T')
AND (Charindex('," & A.ID & ",', F.DefaultVals) > 0)
Thanks in advance!
November 16, 2005 at 3:28 pm
if it is only commas that is in the field cound you not use replace and join on the values without the commas?
November 17, 2005 at 9:00 am
Not sure how to add the replace into the mix - can you give me an example of how it would work with this statement ? How can I join the 2 fields after using replace? Thanks!
November 17, 2005 at 9:38 am
i would fix the table and extract the foreign key, and then add the constrant back tot the table fix the design that is preventing you from doing the right thing with the data.;then fix the application so it inserts the right value into the new FK column.
alternatively, if you can't fix the application to put the values in the right column, i think i would could add a computed column to the table that extracts the id from within the concatenated field, and then join on that;
if you can't fiddle with the schema then stick with extracting the ID as part of your join;
Lowell
November 17, 2005 at 10:21 am
hmm - jumping into this post only to see if anyone will have a solution that doesn't involve redesigning the tables correctly...
Lowell - do you have time to elaborate on "computed column on table that extracts id.."?!?!
**ASCII stupid question, get a stupid ANSI !!!**
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply