Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
declare @ds varchar(25)
set @ds = CONVERT(CHAR(10), GETDATE(),126)
set @ds =  LEFT(@ds, LEN(@ds) - 6)
set @ds = RIGHT(RTRIM(@ds), 2)
select @ds+ '/' + RIGHT('0000' + CONVERT(VARCHAR(5),MAX(CAST(RIGHT(Column_Name,5)AS INT))+ 1),5) DC_No from Table_Name


Sql Answer this
-> 13/00001
there is 13 is the year
and 00001 is the sequence

----
i need that when year change e.g 2013 change in 2014
so my sequence start from initial number e.g 00001
----
so what should i do here......??
Posted
Comments
Shanalal Kasim 10-Apr-13 1:59am    
Provide table structure
saimm 10-Apr-13 5:32am    
ok this is my table

Coulmn name | DataType

CustomerID | varhcar(10) | Not Allow Null
Maciej Los 10-Apr-13 2:07am    
saimm 10-Apr-13 5:30am    
Maciej Los @ there is question even though query same
saimm 10-Apr-13 5:45am    
There is different Question even though Query Same

Try this,

SQL
declare @ds varchar(25)
set @ds = CONVERT(CHAR(10), GETDATE(),126)
set @ds =  LEFT(@ds, LEN(@ds) - 6)
set @ds = RIGHT(RTRIM(@ds), 2)
if (select count(*) from [Table_2] where LEFT(col1,2) = @ds  ) > 0
    select @ds+ '/' + RIGHT('0000' + CONVERT(VARCHAR(5),MAX(CAST(RIGHT(col1,5)AS INT))+ 1),5) DC_No from [Table_2]

Else
    select @ds+ '/' + '00001' DC_No
 
Share this answer
 
Comments
saimm 10-Apr-13 6:26am    
Thanks Shanlal Kasim @ grt work friend
wonder Query
Shanalal Kasim 10-Apr-13 6:27am    
Thanks. Please vote.
your table_name should contain an additional column (called ds?) where you would store the year. In this case the select statement will be:

SQL
select @ds+ '/' + RIGHT('0000' + CONVERT(VARCHAR(5),MAX(CAST(RIGHT(Column_Name,5)AS INT))+ 1),5) DC_No from Table_Name where ds = @ds
 
Share this answer
 
Comments
saimm 10-Apr-13 5:42am    
Oh !!!!

there is i generate new customer code
like this

insert into table 1 (column1) values (select column1 from table 2 where id = 4);

means this is a sub query
hope u understand

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