Solved

Create Unique Reference on SQL Column

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

830 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