?
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
Medium Priority
?
480 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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 2000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

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.
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.
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.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

777 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