Solved

Incrementing a string Values in SQL Server

Posted on 2009-07-08
7
431 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Want an individual results display div 8 40
SQL Server Resume 5 44
Need SSIS project 2 23
SQL Distinct Question 3 12
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.

713 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