Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All

I have build table name sale_invoice. I want one increment in my ID field.
Like This...

LE001001
LE001002
LE001003
LE001004
.
.
.
.
So on..

Can any one help me please

Thanks in Advance
Posted

You may want to try this also

SQL
Create table  #temp
(
Id int identity(1001,1),
user_code as 'LE'+REPLICATE('0',6-Len(Id))+Convert(Varchar(10),Id)
)


INSERT INTO #temp DEFAULT VALUES
INSERT INTO #temp DEFAULT VALUES
INSERT INTO #temp DEFAULT VALUES
 
Share this answer
 
Comments
CHill60 6-Jan-15 4:26am    
Reason for my vote of 5 - neat, elegant, correct way (IMHO) if you want the code on the database and not the presentation layer.
Shweta N Mishra 6-Jan-15 4:44am    
Thank you :)
Simple ,Just Create a Table Design like below:

SQL
create table  Table1
(
Id int identity(1,1),
user_code as case  len(Id) when 1 then 'LE00100'+convert(varchar,ID)
when 2 then 'LE0010'+convert(varchar,ID)
when 2 then 'LE001'+convert(varchar,ID)
else 'LE'+convert(varchar,ID)
end
)

Test:
INSERT INTO Table1 DEFAULT VALUES;
INSERT INTO Table1 DEFAULT VALUES
INSERT INTO Table1 DEFAULT VALUES
INSERT INTO Table1 DEFAULT VALUES
INSERT INTO Table1 DEFAULT VALUES;

select *From Table1 
 
Share this answer
 
Comments
CHill60 6-Jan-15 4:25am    
Reason for my vote of 3 - along the right lines with the computed column but over-engineered with the case statement.
Here is a method using the sequence object.
Works for MS SQL server 2012 and higher.
SQL
create sequence SQforLE
    as int
	start with 1
    increment by 1
;
go

create table SQTable(
	Id varchar(10) constraint DFSQforLE default 'LE'+replace(str((next value for SQforLE), 6), space(1), '0')
);

--insert default data
declare @cnt int; set @cnt=100;
while @cnt <> 0 begin
	insert into SQTable default values;
	set @cnt=@cnt-1;
end;

--get the result
select * from SQTable;

About sequence: http://msdn.microsoft.com/en-gb/library/ff878091(v=sql.110).aspx[^]
Have a read of the following for the use of sequence and limitations http://msdn.microsoft.com/en-gb/library/ff878058(v=sql.110).aspx[^]
 
Share this answer
 
Hi Try this

SQL
CREATE FUNCTION NextNumber()
RETURNS Char(8)
AS
BEGIN
declare @lastValue Char (8)
Declare @result Char (8)
set @lastvalue = (select max(custNumber) from Customers) 	
if @lastvalue is null 
set @lastvalue = 'LE001001' 
Declare @cnt integer
set @cnt = right(@lastvalue , 4) + 1 
select @result = 'LE00' + convert(varchar(8), @cnt) 	

RETURN @result
END
 
Share this answer
 
v3
Comments
CHill60 6-Jan-15 4:21am    
Reason for my vote of 1 - this can only work consistently and safely in a single-user environment and is also not performant.
Sumit Jawale 6-Jan-15 6:29am    
Yes, but I think the question is for getting increment varchar value. So while user want to increase it he will simply call the Function. And thi swill work fine in this scenario.
CHill60 6-Jan-15 6:32am    
We don't know enough about the scenario - nothing in the post indicates that this is a single-user environment
Sumit Jawale 6-Jan-15 6:39am    
if scenario is not clear, then why you voted my answer 1 ?
CHill60 6-Jan-15 6:45am    
I explained why (which is more than most members do). Explicitly max(custNumber) is not guaranteed to return the last customer number inserted so I believe it to be an inappropriate solution; This approach can be quite time consuming; you did not explain to the OP where to call this function.
My vote of 1 was not a personal attack - I just ranked the presented solutions as a guide to the OP (and others)

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