SQL Server | Other
Additional SQL Server features and topics not covered by specific categories
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Is there any function that reverse the whole column?
Sample like this:
Result:
Additional SQL Server features and topics not covered by specific categories
Answer accepted by question author
pretty simple set operation:
update test
set value = t2.value
from (
select id, ROW_NUMBER() over (order by id) as rn
from test
) t1
join (
select id, value, ROW_NUMBER() over (order by id desc) as rn
from test
) t2 on t1.rn = t2.rn
Hi @Lylyy,
Please try the following.
-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, [value] INT);
INSERT @tbl ([value]) VALUES
(111), (222), (333);
-- DDL and sample data population, end
-- before
SELECT * FROM @tbl;
-- after
SELECT ROW_NUMBER() OVER (ORDER BY [value] DESC) AS ID
, [value]
FROM @tbl;