Plus or Minus

  • I cant think this morning. I want to query a table, where Rowid is plus or minus 2 of my target rowid. I cant think....can someone help?

    I know its a simple stupid thing.

    Much appreciated

    Steve

  • Sorry, I didn't quite get what you want.

    RowID?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • I have a target RowID held in a variable, I need to return the four rows that are + or - 2 of that target RowID. I know its simple....just cant think at all

  • Oh, that's easy.

    SELECT SomeFields FROM SomeTable

    WHERE RowID BETWEEN @RowID-2 and @RowID +2

    It'll return up to 5 rows. If you don't want the equality condition, then add the following:

    AND RowID != @RowID

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Thanks....bad day here at the office!!

  • LOL. I know the feeling.

    Good luck.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply