In the warehouse , they scan the products to know the details of it. One such is Lot number. Here for a product , this lot number is 4 digit and comes in this format 'YYWW' eg: 0914 , 1003, 0842 . In this the first 2 digit is the year and last 2 digit represents the number of the week of that particular year. So i was asked to retrieve the date i.e the last day of that particular week is going to be the manufacturing date and 2 yrs from that date will be expiration date. When the lot number is scanned it triggers to run this query and returns both manufacturing date and expiration date. The point is i have the block (shown below with an example '0942'and this number is not constant) and i need to create a function for this block to make it a proper query so that when the trigger is called it will return the manufacturing and expiry date. Plz guide me.
declare @yywk varchar(5)
set @yywk = '0942'
declare @k int
set @k = datalength(@yywk)
declare @s int
set @s = cast(substring(@yywk,0,3) as int)
declare @d int
set @d = CASE @s
when 20 then -4
when 19 then -3
when 18 then -2
when 17 then -1
when 16 then -6
when 15 then -5
when 14 then -4
when 13 then -3
when 12 then -1
when 11 then -7
when 10 then -6
when 09 then -5
when 08 then -3
when 07 then -2
when 06 then -1
when 05 then -7
when 04 then -5
when 03 then -4
when 02 then -3
when 01 then -2
when 00 then -7
else 0
end
declare @yy int
set @yy = cast('20' + substring(@yywk,0,3) as int)
declare @ww int
set @ww = cast(substring(@yywk,3,@k) as int)
select
dateadd (week, @ww,dateadd(year,(@yy-1900),0)) + @d As 'Mandate',
dateadd(yy,2,dateadd (week, @ww,dateadd(year,(@yy-1900),0))+ @d) AS 'ExpDate'