Click here to Skip to main content
15,887,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want sql query for reading data

1080200032060120121019130214 this is one id like in my table having 13 ids. just for understanding i am giving like this

108020003 20601 20121019130214
108020003 20602 20121019130734
108020003 20603 20121019131157
108020003 20604 20121019131714
108020003 20605 20121019132137
108020003 20606 20121019141036
108020003 20607 20121019141451
108020003 20608 20121019141957
108020003 20609 20121019142735
108020003 20610 20121019144434
108020003 20611 20121019145003
108020003 20612 20121019145450
108020003 20613 20121019150619

id datatype is nvarchar

i am giving like this format. based on 20601 to 20613 read data.

i am giving id as 10802000320607 , i want data giving id with before two fields and after two fields
108020003 20605 20121019132137
108020003 20606 20121019141036
108020003 20607 20121019141451
108020003 20608 20121019141957
108020003 20609 20121019142735

i am using bellow query, it displays errors.
SQL
Select top 3 from table
where id > @givenId
order by id
 
union
 
select top 2 from table
where id < @givenid
order by id desc

plz help me.
Posted
Updated 17-Apr-13 18:55pm
v2

1 solution

This will give desired result
SQL
Declare @givenId numeric(18,0)
set @givenId=10802000320607
Select id from table
where convert(numeric(18,0),left(id,14)) between @givenId-2 and @givenId+2
order by id

Happy Coding!
:)
 
Share this answer
 
Comments
subbu a 18-Apr-13 1:58am    
It is working. this is very helped to me. Thanks.
Aarti Meswania 18-Apr-13 2:00am    
Welcome!
Glad to help you! :)
when you get solution press "Accept solution" button for working answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900