Click here to Skip to main content
15,867,961 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I work on sql server 2012 i need to get featurekey and feature value separated $
Based on partid but i don't know how o do that by select sql query ?
expected result as below

SQL
PartId	Featurekey	 FeatureValue
1550	Botato$Mango$dates	Yellow$Red$Black
1600	Rice$macrona$chicken	white$Red$Yellow
1700	Guava$grapes$FIG	Yellow$Green$Red


What I have tried:

SQL
sample data

 create table #PartsFeature
 (
 PartId int,
 Featurekey nvarchar(200),
 FeatureValue nvarchar(200),
 )
 insert into #PartsFeature(PartId,Featurekey,FeatureValue)
 values
 (1550,'Botato','Yellow'),
 (1550,'Mango','Red'),
 (1550,'dates','Black'),
 (1600,'Rice','white'),
 (1600,'macrona','Red'),
 (1600,'chicken','Yellow'),
 (1700,'Guava','Yellow'),
 (1700,'grapes','Green'),
 (1700,'FIG','Red')
Posted
Updated 27-Oct-21 21:59pm

1 solution

Concatenating Row Values in Transact-SQL - Simple Talk[^]

SQL
SELECT
	PartId,
	STUFF((SELECT N'$' + Featurekey FROM #PartsFeature As F1 WHERE F1.PartId = F.PartId FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)'), 1, 1, N'') As FeatureKey,
	STUFF((SELECT N'$' + FeatureValue FROM #PartsFeature As F1 WHERE F1.PartId = F.PartId FOR XML PATH(''), TYPE).value('.', 'nvarchar(max)'), 1, 1, N'') As FeatureValue
FROM
	#PartsFeature As F
GROUP BY
	PartId
;
 
Share this answer
 
Comments
Maciej Los 28-Oct-21 15:42pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900