Click here to Skip to main content
15,921,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I want auto increment for id column in sql where id column should be like companyname and autoincrement value.How can i do this..
Posted

USE Identity Property in SQL server Then u can increment.
 
Share this answer
 
Comments
yarakiran 11-Apr-11 2:57am    
column is not having only integers.column is 'companyname' and autoincrement value
anilkumar1986 11-Apr-11 3:01am    
SO IN DATABASE U HAVE TAKEN VARCHAR FOR AUTOINCRIMENT Means Example:1001Infosis like that it is there.
yarakiran 11-Apr-11 3:10am    
yes
anilkumar1986 11-Apr-11 7:00am    
Then u should Conactinate both id and string then only it will come
yarakiran 11-Apr-11 7:07am    
yes i done the same.thanks
What You can do is Concat Id column with Name

select cast(id  as Varchar(30))+ Name as Name1 from Test5 

while retrieving
 
Share this answer
 
Hello,

You can do this by either creating a trigger during insert, or modify the default value for your identity column. Take this as an example.

CREATE FUNCTION CompanyBasedId (@id int, @company varchar(6))
RETURNS varchar(10)
AS
BEGIN
RETURN @company + Right('0000' + Convert(varchar(10), @id), 4)
END


ALTER TABLE YOURTABLENAME ADD CustomId as dbo.CompanyBasedId(id, company)



The script above should have a table structure like this one.

YOURTABLENAME
id (autoincrement int column)
company (company code varchar(6))
customId (your customized autoincrement column thats based on id and company column)


Regards.
 
Share this answer
 
v2
Do not concatenate separate data into a single field. If you must have company name + autoincrement information, use separate columns. For example:
- CustomerName varchar(...)
- Id int  IDENTITY(1,1)
Now if you want to combine these, you can do it in a query:
SELECT CustomerName + CONVERT(varchar, id) ...

Or if you like, you can create a view that does this operation so the querying would be easier.

And one option is to use computed colummns [^].
 
Share this answer
 

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