expected output
fieldname row and column name
Shambhu Rai
1,411
Reputation points
Hi Expert,
i wanted to get the data according to field name
create table main2( testid int ,oldvalue int, newvalue int, fieldname varchar(100),SaleDateId date, quantity int,cons int)
--drop table main2
insert main2
values
(1,1,10,'quantity','20240307',10,1),
(1,2,10,'cons','20240307',1,10),
(2,3,10,'quantity','20240307',10,1),
(2,4,10,'cons','20240307',1,10),
(2,5,10,'con','20240307',1,10)
expected output
![183005-image.png][1]
like this 100s of column available but it should be sorted based on the fieldname and it will come next to field name column order by testid
Developer technologies | Transact-SQL
4,707 questions
SQL Server | Other
14,494 questions
2 answers
Sort by: Most helpful
-
Shambhu Rai 1,411 Reputation points
2022-03-14T20:55:33.143+00:00 -
Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
2022-03-15T22:09:31.96+00:00 In the fieldname rows column name is mentioned that column should be available next to it.
The order of how the column appears is determined when you write the SELECT list. If you are talking about the value, you can do:
SELECT testid, oldvalue, newvalue, fieldname, CASE fieldname WHEN 'quantity' THEN quantity WHEN 'cons' THEN cons END AS ValueNextToFieldName, SaleDateId FROM main2