Solved

I need an Identity column in my table that has a letter prefix.....eg z3456,z3457,z3458...etc How can this be achieved in sql server?

Posted on 2004-09-09
14
477 Views
Last Modified: 2006-11-17
I need an Identity column in my table that has a letter prefix.....eg z3456,z3457,z3458...etc How can this be achieved in sql server?

Please note, I am a beginner.

Your help would be greatly appreciated.

Kind Regards

PG
0
Comment
Question by:pgilfeather
[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
  • 3
  • 3
  • +3
14 Comments
 
LVL 15

Expert Comment

by:mattisflones
ID: 12016119
Why? Make a ordinary identcoloumn and add the Z afterwards..
0
 
LVL 6

Expert Comment

by:OlegP
ID: 12016123
1)at table you must field ID1 int identity(1,1)
2)at table you must field ID2 varchar(20)
3) create trigger- which will be update field ID2 AS 'Z'+ID1
 or make ID2 - calculated field as  'Z'+ID1

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 12016128
Use a normal identitiy column, and create a computed column that concatenates the 'z' with the identity value.

Cheers
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:Hilaire
ID: 12016134
If you need an auto-incremental filed as a primary, I suggest you stock to

an INT IDENTITY datatype.

Then, if need be, you can create another column (a computed column to get the modified ID in a string format whith the heading 'z'

alter table <YourTable> add ComputedID as 'z' + cast(ID as varchar(10)
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 12016153
@angelIII
sorry, simultaneous post ...

@PG
please don't use a trigger for that. No offense to OlegP, but the added value is not worth teh overhead of a trigger
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 12016156
3 comments in the same minute ;-)
To add some information:
keeping the 'Z' and the identity in different columns should make searches and joins more performant.
If you have other tables in YOUR database, they should also basically store the identity value and compute the 'z' part apart...
CHeers
0
 
LVL 6

Expert Comment

by:OlegP
ID: 12016159
the simplest way use view with calculate ID field to Z+ID
0
 

Author Comment

by:pgilfeather
ID: 12016257
Hilaire

Can you elaborate a little bit on 'z' + cast(ID as varchar(10)

I know from this that you know how to do it but can you guide me as to how to actually implement this formula?

Kind Regards

PG

0
 
LVL 9

Expert Comment

by:solution46
ID: 12016260
Agree with AngelIII

Don't worry about the Z in your tables. Use a straightforward identity column and add the Z whenever you generate any output, or in your front end interface.

s46.
0
 
LVL 26

Accepted Solution

by:
Hilaire earned 500 total points
ID: 12016326
Once you have the "normal" identity column (I assumed it's called ID)
the you just have to run the code below ( using your actual table name instead of <Tabel Name>) to create the computed column,

--note there was a missing parenthesis in my previous csample code
alter table <YourTable> add ComputedID as 'z' + cast(ID as varchar(10))

then you can use it as a normal column

select ComputedID from <YourTable>
0
 

Author Comment

by:pgilfeather
ID: 12016375
Oh,

I see where the misunderstanding is happening.

I dont want to generate an ID prefixed with z.

I have two tables that have numeric identity columns Int (4). What I need to do is automatically copy the new ID from one table into the other but with a z prefix.

Hope this makes more sense.

Thanks very much for all your help.

PG
0
 
LVL 6

Expert Comment

by:OlegP
ID: 12016465
use trigger or create sp which will be insert new records, receive ID and insert z+ID to another table or ... many varians
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 12016748
Still we don't understand is WHY you need to 'z' prefix?

Can you post the relevant table structure and explain what you are trying to achieve in plain english
CHeers
0
 

Author Comment

by:pgilfeather
ID: 12017155
Hilaire...you get star prize!! Your solution worked great.

Cheers

PG
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Agent Job Error 13 78
T-SQL: Only Wanting One Record 8 60
Remove () 10 40
ASP.NET, Globalization, language indexes, translation, conversion. 2 64
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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 the fundamental information of how to create a table.

734 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