473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Auto gen record id as alphanumeric values

ak1dnar
1,584 Expert 1GB
I need a query to generate the client id automatically with in my client table.
Currently i can generate it in this format 10001,10002,10003... . but I need
another char value before the numeric value..
ex: C10001, C10002
Is there any way to do it.

Current code:

Expand|Select|Wrap|Line Numbers
  1. create table clients
  2. (
  3. c_id int not null primary key IDENTITY (10001,1),
  4. c_name char(50) not null,
  5. c_address char(80) not null,
  6. c_email char (25)not null,
  7. c_phone char(50) not null,
  8. c_con_person char(25) not null,
  9. c_con_person_phone char(15),
  10. )
  11.  
Feb 16 '07 #1
9 11130
almaz
168 Expert 100+
Do you want to change c_id column from int to varchar??? It is a very bad practice to use non-integer types as a primary key, so I suggest you to live it as is.
Please describe why do you want to use "C" prefix?
Feb 16 '07 #2
ak1dnar
1,584 Expert 1GB
If changing client id field from int to char is a bad idea..let me know this thing.
I'll put the same PK with the table but i want to give another unique ID for the client like C101,C102,C103...

Can i generate this using IDENTITY or is there any other way to do this..
Feb 16 '07 #3
almaz
168 Expert 100+
It depends on whether user is permitted to change this unique ID, or it is a read-only field. If read-only - you can either add "C" to existing client_id identity column in client application, or add a calculated field
Expand|Select|Wrap|Line Numbers
  1. create table #a(id int identity (1,1))
  2. insert #a default values
  3. insert #a default values
  4. insert #a default values
  5. insert #a default values
  6. insert #a default values
  7. insert #a default values
  8. insert #a default values
  9. insert #a default values
  10. insert #a default values
  11. select * from #a
  12.  
  13. alter table #a add userid as 'C' + cast(id as varchar(10))
  14. select * from #a
If user is permitted to change the value, then you can just initialize the value using identity column on row inserts:
Expand|Select|Wrap|Line Numbers
  1. alter table #a add userid2 varchar(10)
  2. insert #a default values
  3. update #a set userid2 = 'C' + cast(id as varchar(10)) where id = scope_identity()
  4. select * from #a
Feb 16 '07 #4
ak1dnar
1,584 Expert 1GB
Thank u so much.
Permission is not a problem i can make it.
Please take a look at for this also.

Expand|Select|Wrap|Line Numbers
  1. create table #b 
  2. (
  3. id int identity (1001,1),
  4. userid as 'C' + cast(id as varchar(10))
  5. )
  6.  
  7. insert #b default values
  8. insert #b default values
  9. insert #b default values
  10. insert #b default values
  11.  
  12. select * from #b
with out going for two columns like in the Query..can't we make the auto gen id in a single column with prefix Chars.

------------
ID
------------
C1
C2
C3
------------
Feb 16 '07 #5
almaz
168 Expert 100+
No, identity columns are integer columns, so you cannot automatically fill in the alphanumeric field. You can also create a script that will search for maximum value and insert next number, but I still don't understand why do you want to get rid of integer ID column...
Feb 16 '07 #6
ak1dnar
1,584 Expert 1GB
Thanks for the reply again.
In my application i have to Automatically generate the Client ID. and the format of the ID should start with a Char value.

How ever i wanted to check whether there is possibility to make it on DB level. so as i feel now i have to make it from the Application level. by using the int value from the DB. anyway thanks again.
Feb 16 '07 #7
I have a similar dilemma...

I have a unique numeric field that identifies each record, but the client wants a different 9 character unique alphanumeric field to capture each record. Any suggestions on where to start?
Mar 14 '07 #8
almaz
168 Expert 100+
Why does client asks for 9 alphanumeric characters? To show it to user (an possibly allow to edit)? If yes - create non-visible integer identity column as primary key, and another one (9 chars, unique constraint) to be shown
Mar 14 '07 #9
The client's database does not allow have a column to hold the 16 character identifier we have. Their database has a 9-character (alphanumeric) column, so they do not want to receive our 16-char number. Instead they want the 9-char identifier. This new field cannot be editted. In a way, it's going to act as a second primary key.


Why does client asks for 9 alphanumeric characters? To show it to user (an possibly allow to edit)? If yes - create non-visible integer identity column as primary key, and another one (9 chars, unique constraint) to be shown
Mar 14 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: John Baker | last post by:
Hi: I know this is a strange question, but I have inherited a system where files are copied and records re auto numbered (as an index field) )frequently, and I am wondering how high the number...
2
by: anita | last post by:
Hello Can anybody pls help me out. I am creating a table with a field that has about 50,000 randomly generated values(RandomID). Everytime I enter a new record with new RecordID, I want the next...
4
by: anita | last post by:
I had posted this question before, but did not hear from anybody. Can somebody pls help me out. I am creating a table with two fields F1, F2 and F2 has about 50,000 randomly generated alphanumeric...
6
by: Alpha | last post by:
I retrieve a table with only 2 columns. One is a auto-generated primary key column and the 2nd is a string. When I add a new row to the dataset to be updated back to the database. What should I...
3
by: James Alba | last post by:
Hey all, I am accessing an ms access database using .NET and C#. Like so, /* Create the database connection. */ connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data...
1
by: DSR | last post by:
Can anyone tell me how to do this? I have a table (TableBuildings) that I want to auto create data to its sub datasheet (TableDaysOfWeek) whenever a new record is added to the parent table (one to...
5
by: Phil Latio | last post by:
I have 2 virtually identical tables and wish to move data between them. Basically one table is called "live_table" and the other is named "suspended_table" and the only difference is that the...
2
by: x0054 | last post by:
Ok, this is a stupid problem, I admit. I have a scrip that adds records to a table. The records are for photos. So, after adding a record the scrip also uploads a picture from users computer and...
2
by: john | last post by:
Is it true that if I split my access database in backend and frontend and I implement custom auto increment for the ID fields, that my database is ready to be used in a multi-user environment? I...
0
chumlyumly
by: chumlyumly | last post by:
Hello scripters - OS: Mac OSX Language: PHP w/ MySQL database I've created an insert page where a user inputs his info, which then goes to four different tables in a MySQL database. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.