Solved

Incrementing a string Values in SQL Server

Posted on 2009-07-08
7
436 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

728 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