add batch
remove batch
split batch
comment selection
show hidden batches
hide batch
highlight batch
db<>fiddle
Db2
Firebird
MariaDB
MySQL
Node.js
Oracle
Postgres
SQLite
SQL Server
TimescaleDB
YugabyteDB
Developer-C 11.1
3.0
4.0
10.2
10.3
10.4
10.5
10.6
10.7
10.8
10.9
5.5
5.6
5.7
8.0
18
11g Release 2
18c
21c
23c
8.4
9.3
9.4
9.5
9.6
10
11
12
13
14
15
16
3.8
3.16
3.27
3.39
2012
2014
2016
2017
2017 (Linux)
2019
2019 (Linux)
2022
2.11
2.6
2.8
2.18
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
Sakila
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
no sample DB
AdventureWorks
no sample DB
no sample DB
AdventureWorks
no sample DB
no sample DB
no sample DB
no sample DB
run
markdown
donate
feedback
about
By using db<>fiddle, you agree to license everything you submit by
Creative Commons CC0
.
create table #partsfeature ( PartId int, FeatureName varchar(300), FeatureValue varchar(300), FeatureString varchar(300) )
insert into #partsfeature(PartId,FeatureName,FeatureValue,FeatureString) values (1211,'AC','5V','AC(%S)Boil(%S)Temp(%S)'), (1211,'Boil','10v','AC(%S)Boil(%S)Temp(%S)'), (1211,'Temp','5V','AC(%S)Boil(%S)Temp(%S)'), (2421,'grail','51V','Alc(%S)Coil(%S)grail(%S)'), (2421,'Coil','9V','Alc(%S)Coil(%S)grail(%S)'), (2421,'Alc','5V','Alc(%S)Coil(%S)grail(%S)'), (6211,'compress','33v','compress(%S)heat(%S)push(%S)'), (6211,'heat','90v','compress(%S)heat(%S)push(%S)'), (6211,'push','80v','compress(%S)heat(%S)push(%S)'), (5442,'compress','33v','compress(%S)heat()push(%S)'), (5442,'heat','90v','compress(%S)heat()push(%S)'), (5442,'push','80v','compress(%S)heat()push(%S)'), (7791,'AC','5V','AC(%S)Boil(%S)'), (7791,'Boil','10v','AC(%S)Boil(%S)'), (7791,'Temp','5V','AC(%S)Boil(%S)')
15 rows affected
SELECT P.*, FVS.FeatureValueString FROM #partsfeature AS P CROSS APPLY ( SELECT FeatureValueString = STRING_AGG( CONCAT( P2.FeatureName, '(', IIF( CHARINDEX( P2.FeatureName + '(%S)', P2.FeatureString) > 0, P2.FeatureValue, ''), ')'), '') WITHIN GROUP ( ORDER BY CHARINDEX(P2.FeatureName, P2.FeatureString)) FROM #partsfeature AS P2 WHERE P2.PartId = P.PartId AND CHARINDEX(P2.FeatureName, P2.FeatureString) > 0 ) AS FVS;
PartId
FeatureName
FeatureValue
FeatureString
FeatureValueString
1211
AC
5V
AC(%S)Boil(%S)Temp(%S)
AC(5V)Boil(10v)Temp(5V)
1211
Boil
10v
AC(%S)Boil(%S)Temp(%S)
AC(5V)Boil(10v)Temp(5V)
1211
Temp
5V
AC(%S)Boil(%S)Temp(%S)
AC(5V)Boil(10v)Temp(5V)
2421
grail
51V
Alc(%S)Coil(%S)grail(%S)
Alc(5V)Coil(9V)grail(51V)
2421
Coil
9V
Alc(%S)Coil(%S)grail(%S)
Alc(5V)Coil(9V)grail(51V)
2421
Alc
5V
Alc(%S)Coil(%S)grail(%S)
Alc(5V)Coil(9V)grail(51V)
6211
compress
33v
compress(%S)heat(%S)push(%S)
compress(33v)heat(90v)push(80v)
6211
heat
90v
compress(%S)heat(%S)push(%S)
compress(33v)heat(90v)push(80v)
6211
push
80v
compress(%S)heat(%S)push(%S)
compress(33v)heat(90v)push(80v)
5442
compress
33v
compress(%S)heat()push(%S)
compress(33v)heat()push(80v)
5442
heat
90v
compress(%S)heat()push(%S)
compress(33v)heat()push(80v)
5442
push
80v
compress(%S)heat()push(%S)
compress(33v)heat()push(80v)
7791
AC
5V
AC(%S)Boil(%S)
AC(5V)Boil(10v)
7791
Boil
10v
AC(%S)Boil(%S)
AC(5V)Boil(10v)
7791
Temp
5V
AC(%S)Boil(%S)
AC(5V)Boil(10v)
WITH R AS ( SELECT DISTINCT P.PartId, P.FeatureString FROM #partsfeature AS P UNION ALL SELECT P.PartId, CONVERT(varchar(300), REPLACE( R.FeatureString, P.FeatureName + '(%S)', P.FeatureName + '(' + P.FeatureValue + ')')) FROM R JOIN #partsfeature AS P ON P.PartId = R.PartId AND R.FeatureString LIKE '%' + P.FeatureName + '([%]S)%' AND R.FeatureString NOT LIKE '%([%]S)%' + P.FeatureName + '%' ) SELECT P.*, FeatureValueString = R.FeatureString FROM R JOIN #partsfeature AS P ON P.PartId = R.PartId WHERE R.FeatureString NOT LIKE '%([%]S)%';
PartId
FeatureName
FeatureValue
FeatureString
FeatureValueString
1211
AC
5V
AC(%S)Boil(%S)Temp(%S)
AC(5V)Boil(10v)Temp(5V)
1211
Boil
10v
AC(%S)Boil(%S)Temp(%S)
AC(5V)Boil(10v)Temp(5V)
1211
Temp
5V
AC(%S)Boil(%S)Temp(%S)
AC(5V)Boil(10v)Temp(5V)
2421
grail
51V
Alc(%S)Coil(%S)grail(%S)
Alc(5V)Coil(9V)grail(51V)
2421
Coil
9V
Alc(%S)Coil(%S)grail(%S)
Alc(5V)Coil(9V)grail(51V)
2421
Alc
5V
Alc(%S)Coil(%S)grail(%S)
Alc(5V)Coil(9V)grail(51V)
6211
compress
33v
compress(%S)heat(%S)push(%S)
compress(33v)heat(90v)push(80v)
6211
heat
90v
compress(%S)heat(%S)push(%S)
compress(33v)heat(90v)push(80v)
6211
push
80v
compress(%S)heat(%S)push(%S)
compress(33v)heat(90v)push(80v)
5442
compress
33v
compress(%S)heat()push(%S)
compress(33v)heat()push(80v)
5442
heat
90v
compress(%S)heat()push(%S)
compress(33v)heat()push(80v)
5442
push
80v
compress(%S)heat()push(%S)
compress(33v)heat()push(80v)
7791
AC
5V
AC(%S)Boil(%S)
AC(5V)Boil(10v)
7791
Boil
10v
AC(%S)Boil(%S)
AC(5V)Boil(10v)
7791
Temp
5V
AC(%S)Boil(%S)
AC(5V)Boil(10v)