Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
shawnmolloy
Yak Posting Veteran
93 Posts |
Posted - 2007-05-21 : 01:34:53
|
| I have a table that holds images. I want to get the previous image id and the next image id based on the current ID:ImageID421567499So if the current image ID is "21" I'd like to return 4 and 56.Thanks!! |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-21 : 01:37:41
|
[code]select prev_id = max(ImageID)from tblwhere ImageID < 21select next_id = min(ImageID)from tblwhere ImageID > 21[/code] KH |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-05-21 : 01:47:22
|
or in single queryselect prev_id = max(case when ImageID < 21 then ImageID end), next_id = min(case when ImageID > 21 then ImageID end)from tbl KH |
 |
|
|
shawnmolloy
Yak Posting Veteran
93 Posts |
Posted - 2007-05-21 : 02:38:49
|
| Thank you! That worked great. |
 |
|
|
|
|
|