SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Custom numbering field based on year
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

yiotaa
Starting Member

4 Posts

Posted - 06/14/2012 :  02:53:07  Show Profile  Reply with Quote
Hi! I am trying to create a numbering field in a table but i want to be reset every year.
I would like to have this format yyyy/00000
e.g
2010/00001
2010/00002
2010/00003
2011/00001
2011/00002
2012/00001
2012/00002
2012/00003

Can you please help me????????????????????

khtan
In (Som, Ni, Yak)

Singapore
16769 Posts

Posted - 06/14/2012 :  03:05:19  Show Profile  Reply with Quote

row_number() over (partition by [year column] order by [some column])



KH
Time is always against us

Go to Top of Page

yiotaa
Starting Member

4 Posts

Posted - 06/14/2012 :  03:25:55  Show Profile  Reply with Quote
Can you please explain me with more details what you mean?
Go to Top of Page

yiotaa
Starting Member

4 Posts

Posted - 06/14/2012 :  03:35:05  Show Profile  Reply with Quote
I am using this trigger
REATE TRIGGER insertReceiptNoLong ON dbo.RECEIPTS
after INSERT
AS
Declare
@longID varchar(50),
@idLength int,
@seedValue varchar(5)



--Check if the seed is 5 digits long
set @idLength = 5 - (select len(convert(varchar,@@IDENTITY)))
set @seedValue = convert(varchar,@@IDENTITY)
--if the seed length is less than file padd extra zeroes on left side
while @idLength > 0
Begin
set @seedValue = '0' + @seedValue
set @idLength = @idLength - 1
End


--join everything together
set @longID = convert(varchar,year(getdate())) + '/'+ @seedValue
update RECEIPTS set ReceiptNoLong = @longID where RECEIPTNo = @@IDENTITY

The RECEIPTNo is an autonumbering filed in the table RECEIPTS.
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16769 Posts

Posted - 06/14/2012 :  03:46:08  Show Profile  Reply with Quote
Do you need to store that in your table or just need to generate a running number during retrieval ?





KH
Time is always against us

Go to Top of Page

yiotaa
Starting Member

4 Posts

Posted - 06/14/2012 :  08:24:09  Show Profile  Reply with Quote
i want to store it in the field ReceiptNoLong
Go to Top of Page

khtan
In (Som, Ni, Yak)

Singapore
16769 Posts

Posted - 06/14/2012 :  09:12:03  Show Profile  Reply with Quote
take a look http://www.sqlteam.com/article/custom-auto-generated-sequences-with-sql-server


KH
Time is always against us

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000