ASPForums.Net RSS Feed additions to the content that appears on ASPForums.Net(c) 2019 All rights reserved.Query to get First and Last record from Table in SQL Server<p>Hi <a class="username" rel="amar"> amar</a>,</p> <p>Refer below sample query.</p> <p><span style="text-decoration: underline;"><strong>SQL</strong></span></p> <pre class="brush: sql">DECLARE @tblTest AS TABLE(InvoiceNum NVARCHAR(MAX)) INSERT INTO @tblTest VALUES(&#39;INV001&#39;) INSERT INTO @tblTest VALUES(&#39;INV002&#39;) INSERT INTO @tblTest VALUES(&#39;INV003&#39;) INSERT INTO @tblTest VALUES(&#39;INV004&#39;) SELECT TOP 1 InvoiceNum FirstInvoice, (SELECT TOP 1 * FROM @tblTest ORDER BY InvoiceNum DESC) LastInvoice FROM @tblTest</pre> <p><span style="text-decoration: underline;"><strong>Output</strong></span></p> <table border="1"> <tbody> <tr> <td><strong>FirstInvoice</strong></td> <td><strong>LastInvoice</strong></td> </tr> <tr> <td>INV001</td> <td>INV004</td> </tr> </tbody> </table>, 20 Aug 2019 07:10:35 GMT