Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Get Previous number

Get Previous number

Scheduled Pinned Locked Moved Database
databasesql-serversysadminquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SatyaKeerthi15
    wrote on last edited by
    #1

    I have select query and I am getting results like 2 4 6 If I Pass 6 as Parameter then i need to get previous number of 6 i,e 4. If 9 then it has to give 0. How can I write the query for this in Sql Server 2005

    M 1 Reply Last reply
    0
    • S SatyaKeerthi15

      I have select query and I am getting results like 2 4 6 If I Pass 6 as Parameter then i need to get previous number of 6 i,e 4. If 9 then it has to give 0. How can I write the query for this in Sql Server 2005

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      DECLARE
      @ID INT

      SET @ID = 4

      SELECT TOP 1 ID
      FROM tablename
      WHERE ID < @ID
      ORDER BY ID DESC

      Test the result for null and replace with 0 OR Put the results into a variable in the procedure and test the variable and return the results.

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups