Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 displaying multiple data from a field with inner j

Author  Topic 

svibuk
Yak Posting Veteran

62 Posts

Posted - 2013-01-28 : 05:53:09
i hve multiple tables joined using inner join to the main table

but one of the field in the main table details has a field ( languages )having data as (3,7,8)

i need to display the corresponding language

select l.LANGuage from M_Language l where l.LID in(SELECT LANGUAGES FROM details WHERE memberid='000005' )

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-28 : 06:12:49
the query looks fine. didnt understand issue you're facing

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-01-28 : 06:18:49
I think you have comma separated values in a field named 'LANGUAGES'...

GO
CREATE FUNCTION [dbo].[CustomSplit] (@sep char(1), @s varchar(512))
RETURNS table
AS
RETURN (
WITH Pieces(n, start, stop) AS (
SELECT 1, 1, CHARINDEX(@sep, @s)
UNION ALL
SELECT n + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)
FROM Pieces
WHERE stop > 0
)
SELECT n,
SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS s
FROM Pieces
)

GO
select l.LANGuage
from M_Language l
where l.LID in(SELECT s
FROM details
CROSS APPLY [dbo].[CustomSplit](',',languages)
WHERE memberid='000005')


--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-28 : 06:24:53
you can do this too if you dont want to use a UDF

select l.LANGuage
from M_Language l
INNER JOIN details d
ON ',' + d.LANGUAGES + ',' LIKE '%,' + CAST(l.LID AS varchar(10)) + ',%'


this will work fine for small datasets

Also learn about normalisation and first normal form in RDBMS and make sure you avoid such designs in future.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

svibuk
Yak Posting Veteran

62 Posts

Posted - 2013-01-28 : 09:41:35
thanks for pointing abt the table structure

but culd u explain how it shld be
the tbl struvture i hve is

DETAILS TABLE
MEMBERID,NAME,HEIGHT,WEIGHT,LANG_kNOWN,CITY,COUNTRY

HEIGHT_MASTER
HID,HEIGHT

WEIGHT_MASTER
WID,WEIGHT

LANGUAGE_MASTER
LID,LANGUAGE

populating the list/dropdown controls with master tables
and storing the selected data in DETAILS
ONLY language has multiple selection , wht wuld be the best way


Also learn about normalisation and first normal form in RDBMS and make sure you avoid such designs in future.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/


[/quote]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-28 : 10:00:10
best way would be to resolve many to one relationship by means of intermediate table which has languageid against memberid stored as rows instead of delimited format

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

svibuk
Yak Posting Veteran

62 Posts

Posted - 2013-01-30 : 00:36:15
quote:
Originally posted by visakh16

best way would be to resolve many to one relationship by means of intermediate table which has languageid against memberid stored as rows instead of delimited format

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/






Thanks for pointing out the normalization error which i had
i have done accordingly
but the issue is when i am displaying the records in a label control , i need it as delimited
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-30 : 03:12:38
quote:
Originally posted by svibuk

quote:
Originally posted by visakh16

best way would be to resolve many to one relationship by means of intermediate table which has languageid against memberid stored as rows instead of delimited format

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/






Thanks for pointing out the normalization error which i had
i have done accordingly
but the issue is when i am displaying the records in a label control , i need it as delimited


you can always get it in delimited format using FOR XML PATH method



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -