Solved

Incrementing a string Values in SQL Server

Posted on 2009-07-08
7
418 Views
Last Modified: 2012-05-07
Hi,

How can I increment a string value as below...

CU00000001
CU00000002
CU00000003
CU00000004
CU00000005
CU00000006
CU00000007
CU00000008
CU00000009
CU00000010
CU00000011
.
.
.
CU00000019
CU00000020
.
.
.
CU00000100

How i want this is... Everytime when i insert a value I will the check the previous or highest value from the table and then I need to increment it by 1. Then I will be inserting it in the table.

Could any one help me. Thanks in advance.
0
Comment
Question by:Jagdish Devaku
  • 4
  • 2
7 Comments
 
LVL 13

Accepted Solution

by:
Philip Pinnell earned 150 total points
ID: 24802712
something along the lines of

declare @a as varchar(20)
declare @n as int

set @a= 'CU00000100'

set @n = cast(right(@a,8) as int)
set @n = @n+1

set @a = 'CU' + right('0000000000' + cast(@n as varchar(10)),8)
select @a
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 24802725
you could set something like that in a function or maybe just a stored procedure
0
 
LVL 14

Author Comment

by:Jagdish Devaku
ID: 24803045
Thanks Andycrofts... Your solution works.
Can i get any other method to do this. Which will not effect performance of the database. As we are planning to implement this in a very sensitive module.
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 24803188
there may be subtley different way to do it but the bottom line is you have to seperate the bit of the string that can be converted into a number for incremaentation before putting it back together.

The other way of doing it, if you can change the structure of your table so that the key is an auto incrementing identity field and you use a view to build the field that looks like 'CU00000101'

table
column id
id
1
2
3
.
.


view
define your string id as 'CU' + right('00000000' + cast(id as varchar(10)),8)


0
 
LVL 2

Assisted Solution

by:sunil_mails
sunil_mails earned 100 total points
ID: 24803404
--keep a master table "MS_Counters" with 3 columns "CtrType","Prefix" and "postfix"
--This table will allow u maintain multiple counters for multiple tables e.g. emp number, order number etc
create table MS_Counters
(CtrType varchar(10),
Prefix varchar(10),
postfix int)

--Insert dummy value for testing purpose
insert MS_Counters
values('Employee','CU',121)



begin tran -- to ensure evthing is withi transaction and to help lock hints to work

declare @MyCTR int
declare @prfx varchar(2) --, @EmpName varchar(10)

select @prfx =Prefix , @MyCTR=postfix
from MS_Counters with (readpast, updlock) --Lock hints to ensure that different connection doesn't get same number causing duplicate in main table
where CtrType='Employee' --U can decide ur own Ctr types

insert employees(Id,Name)
select @prfx + replicate('0', 8 - len(@MyCTR)) + cast(@MyCTR as varchar), @EmpName

update MS_Counters
set postfix = postfix + 1 --Increment it by 1 to ensure serial number
where CtrType='Employee'


commit tran -- Commit whole operation

Assuming this insert operation will be done in SP, it will ensure ur serial numbers are never duplicate and also it length will always be 10.


0
 
LVL 14

Author Closing Comment

by:Jagdish Devaku
ID: 31601063
Thanks for the support.
0
 
LVL 13

Expert Comment

by:Philip Pinnell
ID: 24897978
Thanks

0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

770 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