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.
| Author |
Topic |
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2009-12-30 : 05:56:20
|
| I have a database full of people that speak other languages, there is a lookup table that has the username and the language id of the language they speak. Some speak many.My currect query I have a function that concats the languages together, like this..User | EN / ES / ITWhat I want is to return those values to multiple columns likeUser | EN | ES | ITso that it can be exported to excel and sorted and filtered. There are dozens more columns than just those 2, but is there a possible way to achieve my goal? |
|
|
Sachin.Nand
2937 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-12-30 : 07:47:06
|
| [code]DROP TABLE #tempDECLARE @str TABLE(userid INT,DATA VARCHAR(128))INSERT INTO @str SELECT 1,'EN/IT/FR/ES/NA'INSERT INTO @str SELECT 2,'EN/IT'INSERT INTO @str SELECT 3,'EN/IT /NA'SELECT userid, REPLACE(SUBSTRING(DATA,CHARINDEX('/',DATA,v.number),ABS(CHARINDEX('/',DATA,CHARINDEX('/',DATA,v.number)+1)-CHARINDEX('/',DATA,v.number))),'/','')AS VALUEINTO #tempFROM @str AS sINNER JOIN master..spt_values AS v ON v.Type = 'P' AND v.number > 0 AND v.number <= LEN(s.data)WHERE SUBSTRING('/' + s.data, v.number, 1) = '/'DECLARE @Sql VARCHAR(MAX)DECLARE @Cols VARCHAR(MAX)SELECT @cols = ISNULL(@Cols,'')+',['+ VALUE+']'FROM (SELECT DISTINCT VALUE FROM #temp)sSELECT @Sql = 'SELECT userid'+@cols+'FROM #tempPIVOT (MAX(value) FOR value IN ('+STUFF(@cols,1,1,'')+'))p'PRINT @SqlEXEC(@Sql)[/code] |
 |
|
|
|
|
|
|
|