Solved

Incrementing a string Values in SQL Server

Posted on 2009-07-08
7
398 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
Comment Utility
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
Comment Utility
you could set something like that in a function or maybe just a stored procedure
0
 
LVL 14

Author Comment

by:Jagdish Devaku
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 13

Expert Comment

by:Philip Pinnell
Comment Utility
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
Comment Utility
--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
Comment Utility
Thanks for the support.
0
 
LVL 13

Expert Comment

by:Philip Pinnell
Comment Utility
Thanks

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now