ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.SQL query to reverse rows of a column in SQL Server<p>Hi <a class="username" rel="smile"> smile</a>,</p> <p>Refer below test query.</p> <p><strong><span style="text-decoration: underline;">SQL</span></strong></p> <pre class="brush: sql">DECLARE @Test AS TABLE(Name CHAR(50),Descript CHAR(50)) INSERT INTO @Test VALUES(&#39;Coputer&#39;,&#39;Machine&#39;) INSERT INTO @Test VALUES(&#39;Keyboard&#39;,&#39;Input device&#39;) INSERT INTO @Test VALUES(&#39;Monitor&#39;,&#39;Output device&#39;) INSERT INTO @Test VALUES(&#39;Harddisk&#39;,&#39;Storage device&#39;) DECLARE @Counter INT, @TotalCount INT, @Descript CHAR(50) SET @Counter = 1 SET @TotalCount = (SELECT COUNT(*) FROM @Test) SELECT ROW_NUMBER() OVER(ORDER BY(SELECT 0)) RowNum,* INTO #Temp FROM @Test WHILE (@Counter &lt;= @TotalCount) BEGIN SET @Descript = (SELECT Descript FROM (SELECT ROW_NUMBER() OVER(ORDER BY(SELECT 0)) RowNum,Descript FROM @Test)t WHERE t.RowNum = (@TotalCount + 1) - @Counter) UPDATE #Temp SET Descript = @Descript WHERE RowNum = @Counter SET @Counter = @Counter + 1 CONTINUE; END SELECT Name,Descript FROM #Temp DROP TABLE #Temp</pre> <p><strong><span style="text-decoration: underline;">Output</span></strong></p> <table border="1"> <tbody> <tr> <td><strong>Name</strong></td> <td><strong>Descript</strong></td> </tr> <tr> <td>Coputer</td> <td>Storage device</td> </tr> <tr> <td>Keyboard</td> <td>Output device</td> </tr> <tr> <td>Monitor</td> <td>Input device</td> </tr> <tr> <td>Harddisk</td> <td>Machine</td> </tr> </tbody> </table>https://www.aspforums.net:443/Threads/149937/SQL-query-to-reverse-rows-of-a-column-in-SQL-Server/https://www.aspforums.net:443/Threads/149937/SQL-query-to-reverse-rows-of-a-column-in-SQL-Server/Thu, 04 Oct 2018 03:30:00 GMT