I want Date wise dynamically generate course ID
Example:
Last week generate end Batch ID: CSE100,CSE101,....CSE199
Now This week Start date Dynamically(Automatically) generate Batch ID:CSE200
Thanks..
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Wim SturkenboomRank #2,527 · 554 reputationPosted Sep 17, 2014, 9:41 AM EDT
declare @lastweek int
declare @currweek int
declare @lastid int
declare @newid varchar(50)
-- get weeknumber of last insert
set @lastweek = (select DATEPART (wk, (select top(1) BatchDate from tblBatch order by BatchDate desc)))
-- get current week number
set @currweek = (select DATEPART (wk, CURRENT_TIMESTAMP))
if @currweek > @lastweek
begin
-- get the last inserted id
set @lastid = (select top (1) SUBSTRING(BatchID, LEN(BatchName)+1, LEN(BatchID) -3) from tblBatch order by BatchDate desc)
-- create new id
set @newid = (select top(1) BatchName from tblBatch order by BatchDate desc) + CAST(@lastid + 1 as varchar(50))
end
select @lastweek, @currweek, @lastid, @newid
You can put it in a stored procedure and let it return newid.
Note that you need to add a check for the end of the year where last week (52/53) will be larger than currweek. Also note that newID will be null if the current week is less than or equal to lastweek.
I hope this is more or less what you need and get you on the way.
Wim SturkenboomRank #2,527 · 554 reputationPosted Sep 17, 2014, 5:48 AM EDT
Next you don't know when the last ID was generated, so you can't predict if the next id is still in the same week (what would the number be) or in the next week (see question above).
Can you please elaborate more?
Nirmal KumarCRank #6,413 · 267 reputationPosted Sep 17, 2014, 6:00 AM EDT
Batchid BatchName BatchDate Active
CSE199 CSE 13 oct 2014 A
But I want This Week Start 15 oct 2014 automatically generate batch ID CSE200
Thanks..
Nirmal KumarCRank #6,413 · 267 reputationPosted Sep 17, 2014, 8:56 PM EDT