SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 displaying multiple data from a field with inner j
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

svibuk
Yak Posting Veteran

62 Posts

Posted - 01/28/2013 :  05:53:09  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 01/28/2013 :  06:12:49  Show Profile  Reply with Quote
the query looks fine. didnt understand issue you're facing

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

Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2218 Posts

Posted - 01/28/2013 :  06:18:49  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 01/28/2013 :  06:24:53  Show Profile  Reply with Quote
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 - 01/28/2013 :  09:41:35  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 01/28/2013 :  10:00:10  Show Profile  Reply with Quote
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 - 01/30/2013 :  00:36:15  Show Profile  Reply with Quote
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

India
52317 Posts

Posted - 01/30/2013 :  03:12:38  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000