x

How to return the searched record along with five next records

If I want to return six values from the middle of the table after passing the where clause and it should return the specified value along with the five next record. As for example: select * from employee where id='E000' then I want to return the record of E000 as well as the next five records of E001,E002,E003,E004,E005. Please help me to do this.
more ▼

asked Apr 16, 2012 at 03:11 AM in Default

imteyazkhan374 gravatar image

imteyazkhan374
10 2 2 2

(comments are locked)
10|1200 characters needed characters left

3 answers: sort voted first

Personally, I do not like such alphanumeric incremented columns. You get more pains than gains unless handled as computed columns etc. In your case, if you would follow Magnus Ahlkvist's very good suggestion (which actually gives the core of the solution i.e. TOP and ORDER BY), then 'E10001' would always be sorted before 'E1001' which would not be correct. So it would always be dependent upon the LOGIC and constraints of your such alphanumeric computation of the column. If I assume that there is one alphabet and then multiple numeric characters, then one way of doing it could be

SELECT TOP 6 * FROM Employee WHERE id >= 'E000' 
ORDER BY CAST(RIGHT(id, LEN(id)-1) AS BIGINT)
But again, that could break anytime if the LOGIC of computation of the column is changed or not the same.
more ▼

answered Apr 16, 2012 at 06:45 AM

Usman Butt gravatar image

Usman Butt
13.9k 6 8 14

(comments are locked)
10|1200 characters needed characters left

Perhaps like this?

SELECT TOP 6 * FROM Employee WHERE id >= 'E000' ORDER BY id
more ▼

answered Apr 16, 2012 at 06:22 AM

Magnus Ahlkvist gravatar image

Magnus Ahlkvist
16.6k 17 20 33

(comments are locked)
10|1200 characters needed characters left

/*check this it might help*/

use AdventureWorks2008R2;

declare @i int;
declare @p int;
declare @q int;
declare @RTable table (id int, name varchar(100));

set @i = (SELECT [ContactTypeID] FROM [AdventureWorks].[Person].[ContactType] where [ContactTypeID] = 5);
set @p = @i;
set @q = @i+5;

while (@p <= @q)
begin
    insert into @RTable
    select [ContactTypeID],Name FROM [AdventureWorks].[Person].[ContactType] where [ContactTypeID] = @p
    set @p = @p + 1;
    if (@p > @q)
       break;
end
select * from @RTable
more ▼

answered Apr 16, 2012 at 05:41 AM

ykrao7 gravatar image

ykrao7
0

That may or may not return the correct records. But it's not how a database should be used. The database already contains the set that the six rows make up. Deconstructing them row by row, and reconstructing them into a table (row by row) is far from the best way to use a database.
Apr 16, 2012 at 06:26 AM Magnus Ahlkvist
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1948

asked: Apr 16, 2012 at 03:11 AM

Seen: 611 times

Last Updated: Apr 16, 2012 at 07:26 AM