Solved

Create Unique Reference on SQL Column

Posted on 2013-06-25
3
198 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 500 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

760 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

19 Experts available now in Live!

Get 1:1 Help Now