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
Solved

Incrementing a string Values in SQL Server

Posted on 2009-07-08
7
425 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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.

860 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