?
Solved

generate incrementing numbers

Posted on 2006-05-04
5
Medium Priority
?
249 Views
Last Modified: 2008-03-17
Hi All,

How can I generate incrementing number with text in the value, in sql2000. For instance an invoice number, SD0008762871.. the next number I would want would be SD0008762872

Is there an easy way to do it???

Thanks
0
Comment
Question by:nzfire
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16604139
SELECT 'SD'+RIGHT('000'+CAST(CAST(RIGHT(urColumn,10) AS BIGINT)+1 AS nVArchar(10)),10)
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 16604226
Here is an example

declare @tab Table (col varchar(300))
insert into @tab SELECT 'SD0008762871'
insert into @tab SELECT 'SD0008762872'
INSERT INTO @tab SELECT 'SD0008762873'
SELECT 'SD'+RIGHT('000'+CAST(CAST(RIGHT(MAX(COL),10) AS BIGINT)+1 AS VArchar(10)),10)
FROM @tab
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16604822
note that you should NOT store this kind of things in a single column, but keep the parts apart, possibly have a computed column for the display value...
0
 
LVL 1

Author Comment

by:nzfire
ID: 16608378
Hi AngelIII

Thats what I have been thinking...

I'll try your code aneeshattingal.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 16609527
create table invoice
( invoice_id int identity ( 209040, 1 )
, invoice_prefix varchar(10) default 'SD'
, invoice_prefix + right ( '0000000000' + cast ( invoice_id as varchar(10) ) , 10) as invoide_display
, <other fields>
)

this way, inserting into the table invoice (without specifying invoice_id, invoice_prefix and invoice_display), those 3 values will be filled in automatically.
0

Featured Post

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question