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. = Incrementing a max value

= Incrementing a max value

Scheduled Pinned Locked Moved Database
question
4 Posts 3 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.
  • D Offline
    D Offline
    Dayekh
    wrote on last edited by
    #1

    Hi all, Let me supply the Code and then explain: DECLARE @vchRequestNumber Integer SELECT @vchRequestNumber = MAX(CONVERT(INT,(ISNULL(vchRequestNumber, 0))))+1 FROM tblRequest Ok, here's the deal.. What works: IF vchRequestNumber is GREATER THAN 0 in the TABLE, the code WORKS.. and therefore I am presented with a value of vchRequestNumber PLUS 1. What Doesn't work: IF vchRequestNumber is NULL, then a ZERO replaces it(which is fine); BUT it is returning 0 instead of 0 PLUS 1. Anyone has any ideas???

    P 1 Reply Last reply
    0
    • D Dayekh

      Hi all, Let me supply the Code and then explain: DECLARE @vchRequestNumber Integer SELECT @vchRequestNumber = MAX(CONVERT(INT,(ISNULL(vchRequestNumber, 0))))+1 FROM tblRequest Ok, here's the deal.. What works: IF vchRequestNumber is GREATER THAN 0 in the TABLE, the code WORKS.. and therefore I am presented with a value of vchRequestNumber PLUS 1. What Doesn't work: IF vchRequestNumber is NULL, then a ZERO replaces it(which is fine); BUT it is returning 0 instead of 0 PLUS 1. Anyone has any ideas???

      P Offline
      P Offline
      perle1
      wrote on last edited by
      #2

      Hi Dayekh, try this: DECLARE @vchRequestNumber INT SELECT @vchRequestNumber = MAX(CONVERT(INT,(ISNULL(vchRequestNumber, 0)))) FROM tblRequest SET @vchRequestNumber = @vchRequestNumber + 1 Greetings Ralph

      D 1 Reply Last reply
      0
      • P perle1

        Hi Dayekh, try this: DECLARE @vchRequestNumber INT SELECT @vchRequestNumber = MAX(CONVERT(INT,(ISNULL(vchRequestNumber, 0)))) FROM tblRequest SET @vchRequestNumber = @vchRequestNumber + 1 Greetings Ralph

        D Offline
        D Offline
        Dayekh
        wrote on last edited by
        #3

        hehehe.. i tried that exact thing right now.. but it doesn't work... I found the problem tho.. when no records exist, i.e. when vchRequestNumber is NULL, the ENTIRE evaluation is returning a NULL... so this is what I had to do: ISNULL(MAX(CONVERT(INT,(ISNULL(vchRequestNumber, 0)))),0)+1 Thank you for the reply!

        P 1 Reply Last reply
        0
        • D Dayekh

          hehehe.. i tried that exact thing right now.. but it doesn't work... I found the problem tho.. when no records exist, i.e. when vchRequestNumber is NULL, the ENTIRE evaluation is returning a NULL... so this is what I had to do: ISNULL(MAX(CONVERT(INT,(ISNULL(vchRequestNumber, 0)))),0)+1 Thank you for the reply!

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Alternatively, you could use COALESCE(vchRequestNumber, 0) + 1

          Deja View - the feeling that you've seen this post before.

          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