Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Numbers to Char Data Type

Status
Not open for further replies.

PNC

MIS
Jun 7, 2001
87
US
Hi,

I have a field of data type char(16) containing numbers, I want to add "1" to it, but I still want to keep the same format.

Code:
SELECT NextStamp, NextStamp + 1
 FROM Defaults
--Returns
0000000000880025	880026
--I want
0000000000880025	0000000000880026

Any ideas on how to do this?

Thanks.
 
Code:
SELECT NextStamp,
STUFF('0000000000000000',
      17-DATALENGTH( CONVERT(VARCHAR(16),NextStamp + 1) ),
      16,
      CONVERT(VARCHAR(16),NextStamp + 1)
     )

Forgive me for mentioning this, but it might have been better to store NextStamp as an integer, possibly an autoincrementing or IDENTITY column. Then when the leading zeros are needed for display in an application, make the application responsible for prepending them.

 
You're the man rac2... I know that NextStamp should have been an int, but I didn't develop the database, I'm just the guy who's stuck with the bad design.

Thx.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top