Problem in getting MAX value from table

  • 9 years ago

    hi all,

    I have a field named  pkLocationCd with size nchar(5). I am using the following sql command to get the max+1 value through this:

    select isnull(max(pkLocationCd)+1,1) from tbLocationMaster

    This works fine for upto 10 records, i.e. I am able to insert 10 records easily, but when I try to insert 11th record it shows me an error. After debugging I found out that the value I am getting is 10 with max+1..........where as it should be getting 11.

    can you suggest me where I am going wrong?

    Regards,

    Royal

  • 9 years ago

    Hi guys,

    I got the solution as soon as I posted this problem. the solution is:

    select max(convert(int,pkLocationCd)+1) from tbLocationMaster

    I only had to convert it to int.

    Regards,

    Royal

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only two kinds of languages: the ones people complain about and the ones nobody uses” - Bjarne Stroustrup