Click here to Skip to main content
16,021,115 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

i have one table operationsdeatil . i will enter all details like patientname, patientid but i want create the operationid automaticall like ope_1,ope_2,,,etc.
thankyou
Posted

Use String.Format[^]

C#
string opertaionID = String.Format("ope_{0}", id);
 
Share this answer
 
Comments
Prasad_Kulkarni 21-Jun-12 7:49am    
My 5!
Maciej Los 21-Jun-12 12:35pm    
Good answer, my 5!
If you are using Microsoft SQL, you cannot do this on the identity column. Auto-increment is only for numeric columns. However, there is hope. What you can do is create an auto-increment column of type int and then create a calculated column that is persisted that takes your string and adds the auto-increment column value on the end of it. The calculated column would look something like this (in the CREATE statement):

SQL
[StringID]  AS ('ope_' + CAST(id AS VARCHAR(20)) PERSISTED


This new column will have the values you want and it can be indexed.
 
Share this answer
 
Comments
Prasad_Kulkarni 21-Jun-12 7:50am    
My 5!
Manas Bhardwaj 21-Jun-12 8:09am    
Good +5!
Manfred Rudolf Bihy 21-Jun-12 9:18am    
Calculated columns! Good one 5+
Maciej Los 21-Jun-12 12:35pm    
Good answer, my 5!
What is wrong with just using an integer field here. There is no benefit or additional information by prefixing a number with a contant string. Strings also use ever increasing space as they grow longer and longer whereas an integer (biginteger) field will always consume the same amount of memory. Next reason not to follow you example is that building indexes from integers is easier than from strings.
The fieldname is already operationid so prefixing the field with a constant string is just a waste of space and time. If it is needed for a GUI though you can always generate that in the client side code ( or server side for ASP ). So all you have to do is use integer or biginteger for your field type and set the property Identity to yes, Start=1, Increment=1.

Using MS SQL T-SQL DDL's CREATE TABLE[^] statement use the optional IDENTITY clause in a column definition:

XML
column_name <data_type>
    [ COLLATE collation_name ]
    [ NULL | NOT NULL ]
    [
        [ CONSTRAINT constraint_name ] DEFAULT constant_expression ]
      | [ seed , ^__i>^__b style="color:red;">increment ) ] [ NOT FOR REPLICATION ]
    ]
    [ ROWGUIDCOL ] [ <column_constraint> [ ...n ] ]

— Brought to you by MSDN SQL Server Reference[^]
Regards,

Manfred
 
Share this answer
 
v4
Comments
Manas Bhardwaj 21-Jun-12 8:09am    
This is correct. +5.
Not really sure if user is referring to SQL table, as there is no reference to database in question. It could be just a UI table with some IDs. :)
Manfred Rudolf Bihy 21-Jun-12 9:18am    
That is correct, but since OP said something about "automatically", I assumed OP meant auto increment columns. Might be wrong though. :)
Thanks for the vote!
Maciej Los 21-Jun-12 12:35pm    
Good answer, my 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900