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. General Programming
  3. Visual Basic
  4. Selecting MAX value from a Database column.

Selecting MAX value from a Database column.

Scheduled Pinned Locked Moved Visual Basic
databasehelptutorialquestion
4 Posts 4 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.
  • J Offline
    J Offline
    jeshra279
    wrote on last edited by
    #1

    Hi, I am using SQL database with VB. The database "company" has 1 column "EmpID" which I filled with 0,1,2,... so on, and "EmpID" column is in string format. Now I want to get the MAX of this "EmpID", for this I wrote a code like this in VB : SELECT MAX(EmpID) From company. I found that out of 0 to 150 EmpID in database, it is selecting MAX value as 9 and sometime as 99. I think there is something wrong with this. Also, If I test it for small data, say from 1 to 15, it is selecting correct MAX value. Can someone help me how to do this? My basic idea is to get the last "EmpID" which I will add and use it as next "EmpID". Thanks R.S.

    D L N 3 Replies Last reply
    0
    • J jeshra279

      Hi, I am using SQL database with VB. The database "company" has 1 column "EmpID" which I filled with 0,1,2,... so on, and "EmpID" column is in string format. Now I want to get the MAX of this "EmpID", for this I wrote a code like this in VB : SELECT MAX(EmpID) From company. I found that out of 0 to 150 EmpID in database, it is selecting MAX value as 9 and sometime as 99. I think there is something wrong with this. Also, If I test it for small data, say from 1 to 15, it is selecting correct MAX value. Can someone help me how to do this? My basic idea is to get the last "EmpID" which I will add and use it as next "EmpID". Thanks R.S.

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      I think that is because your EmpId id not an integer in your database. It something be varchar or something similar. You should change it to int if it can have only integer values. In order to make the query work in current condition, use following:

      SELECT MAX(cast(EmpID as int)) From company

      Note that this is not a good practice.

      50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

      1 Reply Last reply
      0
      • J jeshra279

        Hi, I am using SQL database with VB. The database "company" has 1 column "EmpID" which I filled with 0,1,2,... so on, and "EmpID" column is in string format. Now I want to get the MAX of this "EmpID", for this I wrote a code like this in VB : SELECT MAX(EmpID) From company. I found that out of 0 to 150 EmpID in database, it is selecting MAX value as 9 and sometime as 99. I think there is something wrong with this. Also, If I test it for small data, say from 1 to 15, it is selecting correct MAX value. Can someone help me how to do this? My basic idea is to get the last "EmpID" which I will add and use it as next "EmpID". Thanks R.S.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        It is because your EmpID is stored as text instead of number. The correct datatype to use is 'int', and your EmpID column most probably is 'varchar' datatype.

        1 Reply Last reply
        0
        • J jeshra279

          Hi, I am using SQL database with VB. The database "company" has 1 column "EmpID" which I filled with 0,1,2,... so on, and "EmpID" column is in string format. Now I want to get the MAX of this "EmpID", for this I wrote a code like this in VB : SELECT MAX(EmpID) From company. I found that out of 0 to 150 EmpID in database, it is selecting MAX value as 9 and sometime as 99. I think there is something wrong with this. Also, If I test it for small data, say from 1 to 15, it is selecting correct MAX value. Can someone help me how to do this? My basic idea is to get the last "EmpID" which I will add and use it as next "EmpID". Thanks R.S.

          N Offline
          N Offline
          Niladri_Biswas
          wrote on last edited by
          #4

          Select MAX(cast(EmpId as int)) from TblName

          OR

          Select top 1 EmpId from TblName order by EmpId desc

          :)

          Niladri Biswas

          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