Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Create Unique Reference on SQL Column

Posted on 2013-06-25
3
Medium Priority
?
213 Views
Last Modified: 2013-06-25
Hi Guys

I have a Table called Person.

In Person I have  a column called UniqueRef.

The UniqueRef will start with LA00000001.

I need to create a sequential number.

So the next ref will be LA00000002 etc.

Thanks.
0
Comment
Question by:Shezad Ahmed
3 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 39275015
Why dont you create an identity column and display the column like this 'LA00000001'
0
 
LVL 20

Accepted Solution

by:
dsacker earned 2000 total points
ID: 39275043
Is this SQL Server? If so, here is a little test table that may demonstrate a solution:

DECLARE @TempList TABLE (
    Seq         int         NOT NULL IDENTITY (1, 1),
    UniqueRef   AS
        CONVERT(varchar(10), 'LA' +
        RIGHT('00000000' + CONVERT(varchar(8), Seq), 8)),
    Nbr varchar(3) NOT NULL )

DECLARE @Nbr        varchar(3)
SET @Nbr     = '115'

WHILE @Nbr < '178'
BEGIN
    INSERT INTO @TempList (Nbr) VALUES (@Nbr)
    SET @Nbr = CONVERT(varchar(3), CONVERT(smallint, @Nbr) + 1)
END

SELECT * FROM @TempList

Open in new window

0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39275108
Is this column the primary key? if so then go with anees's suggestion above and make it an identity column and just format the value as needed with something like:

   'LA' + RIGHT('00000000' + UniqueRef, 8)

If this is not the case then I would just include the logic for determine the value of UniqueRef in the procedure that should be handling your insertions in that table.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

916 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