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
476 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
  • 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Suggested Solutions

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

685 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