In the example below I've assumed that the comment field is what you're referring to as prompt.
Build some test data;
IF OBJECT_ID('tempdb..#TestingTable') IS NOT NULL DROP TABLE #TestingTable CREATE TABLE #TestingTable ( ChkID int ,Q1_ID int ,Comment1 varchar(20) ,Q2_ID int ,Comment2 varchar(20) ,Q3_ID int ,Comment3 varchar(20) ) INSERT INTO #TestingTable (ChkID, Q1_ID, Comment1, Q2_ID, Comment2, Q3_ID, Comment3) VALUES (1,11,'Row1 Comment1', 12, 'Row1 Comment2', 13, 'Row1 Comment3') ,(2,21,'Row2 Comment1', 22, 'Row2 Comment2', 23, 'Row2 Comment3') ,(3,31,'Row3 Comment1', 32, 'Row3 Comment2', 33, 'Row3 Comment3') ,(4,41,'Row4 Comment1', 42, 'Row4 Comment2', 43, 'Row4 Comment3')
Which gives you this data set;
ChkID Q1_ID Comment1 Q2_ID Comment2 Q3_ID Comment3 1 11 Row1 Comment1 12 Row1 Comment2 13 Row1 Comment3 2 21 Row2 Comment1 22 Row2 Comment2 23 Row2 Comment3 3 31 Row3 Comment1 32 Row3 Comment2 33 Row3 Comment3 4 41 Row4 Comment1 42 Row4 Comment2 43 Row4 Comment3
Then we can do some trickery with UNPIVOT
SELECT u2.ChkID, u2.Q_ID, u2.Comment FROM #TestingTable tt UNPIVOT ( AnswerID for Q_ID in (Q1_ID, Q2_ID, Q3_ID) ) u1 UNPIVOT ( Comment for Comment_ID in (Comment1, Comment2, Comment3) ) u2 WHERE SUBSTRING(u2.Q_ID,2,1) = RIGHT(Comment_ID,1)
Which gives us this result;
ChkID Q_ID Comment 1 Q1_ID Row1 Comment1 1 Q2_ID Row1 Comment2 1 Q3_ID Row1 Comment3 2 Q1_ID Row2 Comment1 2 Q2_ID Row2 Comment2 2 Q3_ID Row2 Comment3 3 Q1_ID Row3 Comment1 3 Q2_ID Row3 Comment2 3 Q3_ID Row3 Comment3 4 Q1_ID Row4 Comment1 4 Q2_ID Row4 Comment2 4 Q3_ID Row4 Comment3
Which is what I think you're after. You'll have to do a little work on that WHERE clause to fit the use case of 200+ columns (this only works for columns with a single digit length) and you'll probably want to put a clause there on the ChkID too. This should get you on your way if I've understood your question correctly.
If you're not sure how many columns you're going to have, then you'll want to do some dynamic querying to pass through your column list. Something like this;
Test Data (a temp table won't work this time because we're using sys.columns)
IF OBJECT_ID('TestingTable', 'U') IS NOT NULL DROP TABLE TestingTable; CREATE TABLE TestingTable ( ChkID int ,Q1_ID int ,Comment1 varchar(20) ,Q2_ID int ,Comment2 varchar(20) ,Q3_ID int ,Comment3 varchar(20) ,Q200_ID int ,Comment200 varchar(20) ) INSERT INTO TestingTable (ChkID, Q1_ID, Comment1, Q2_ID, Comment2, Q3_ID, Comment3, Q200_ID, Comment200) VALUES (1,11,'Row1 Comment1', 12, 'Row1 Comment2', 13, 'Row1 Comment3', 1200, 'Row1 Comment200') ,(2,21,'Row2 Comment1', 22, 'Row2 Comment2', 23, 'Row2 Comment3', 2200, 'Row1 Comment200') ,(3,31,'Row3 Comment1', 32, 'Row3 Comment2', 33, 'Row3 Comment3', 3200, 'Row1 Comment200') ,(4,41,'Row4 Comment1', 42, 'Row4 Comment2', 43, 'Row4 Comment3', 4200, 'Row1 Comment200')
Dynamic version of the query above
DECLARE @sql nvarchar(max) DECLARE @QuestionList nvarchar(max) DECLARE @CommentList nvarchar(max) SELECT @QuestionList = COALESCE(@QuestionList+',' , '') + c.name FROM sys.columns c WHERE c.object_id = OBJECT_ID('TestingTable') AND c.name LIKE 'Q%' AND c.name LIKE '%_ID' SELECT @CommentList = COALESCE(@CommentList+',' , '') + c.name FROM sys.columns c WHERE c.object_id = OBJECT_ID('TestingTable') AND c.name LIKE 'Comment%' SET @sql = N'SELECT u2.ChkID, u2.Q_ID, u2.Comment FROM TestingTable tt UNPIVOT ( AnswerID for Q_ID in ('+@QuestionList+') ) u1 UNPIVOT ( Comment for Comment_ID in ('+@CommentList+') ) u2 WHERE SUBSTRING(Q_ID,2,CHARINDEX(''_'',Q_ID)-2) = SUBSTRING(u2.Comment,CHARINDEX(''Comment'',u2.Comment)+7,3)' EXEC (@sql)
Output
ChkID Q_ID Comment 1 Q1_ID Row1 Comment1 1 Q2_ID Row1 Comment2 1 Q200_ID Row1 Comment200 1 Q3_ID Row1 Comment3 2 Q1_ID Row2 Comment1 2 Q2_ID Row2 Comment2 2 Q200_ID Row1 Comment200 2 Q3_ID Row2 Comment3 3 Q1_ID Row3 Comment1 3 Q2_ID Row3 Comment2 3 Q200_ID Row1 Comment200 3 Q3_ID Row3 Comment3 4 Q1_ID Row4 Comment1 4 Q2_ID Row4 Comment2 4 Q200_ID Row1 Comment200 4 Q3_ID Row4 Comment3
I've updated that last query to allow for Q_ID to go up into the hundreds, you may need to alter this depending upon your exact data structure.
Rollback;Rollback;Rollback;