Solved

Create Unique Reference on SQL Column

Posted on 2013-06-25
3
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remove () 10 41
SQL Percentage Formula 7 33
Need more granular date groupings 4 45
relocating SQL 2000 18 35
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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…

738 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