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
475 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 142

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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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 142

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 142

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I Need a T-SQL Query (Union or Intersect, or...?) 6 39
string fuctions 4 25
VBScript Write Column Headers 3 36
convert null in sql server 12 32
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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

776 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