• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

How to increment an integer with SQL?

How can I increment an integer which is stored in the database as for example "00006".

When I tried to update and did:

set amount = amount + 1

the update was as 7 instead of 00007.

Can you help please?

thanks.

0
Rassac
Asked:
Rassac
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this should do:

set amount = right( '00000' + cast(cast(amount as int) + 1  as varchar(10)), 6)

Open in new window

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
sorry, that was mssql :(
TO_CHAR( amount + 1, '000000')

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now