# Need to Create function for this block

 0 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' more ▼ asked Jan 21, 2010 at 10:21 PM in Default Suresh 11 ● 1 ● 1 ● 1 Rob Farley 5.7k ● 15 ● 18 ● 20 add new comment (comments are locked) 10|1200 characters needed characters left ▼ Viewable by all users

 0 This should do the trick: /* **************************************************************************** SELECT * FROM dbo.DoDatesFromYW('0942') **************************************************************************** */ CREATE FUNCTION dbo.DoDatesFromYW ( @yywk VARCHAR(5) ) RETURNS @TblSub TABLE ( Mandate DATETIME, ExpDate DATETIME ) AS BEGIN DECLARE @k INT DECLARE @s INT DECLARE @d INT DECLARE @yy INT DECLARE @ww INT SET @k = DATALENGTH(@yywk) SET @s = CAST(SUBSTRING(@yywk, 0, 3) AS 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 SET @yy = CAST('20' + SUBSTRING(@yywk,0,3) AS INT) SET @ww = CAST(SUBSTRING(@yywk,3,@k) AS INT) INSERT INTO @TblSub (ManDate, ExpDate) VALUES (DATEADD (week, @ww,DATEADD(year,(@yy-1900),0)) + @d, DATEADD(yy,2,DATEADD (week, @ww,DATEADD(year,(@yy-1900),0))+ @d)) RETURN END You did all the hard work. more ▼ answered Feb 05, 2010 at 11:02 PM dvroman 1.1k ● 2 ● 2 add new comment (comments are locked) 10|1200 characters needed characters left ▼ Viewable by all users

### New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

By Email:

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x1945
x408
x57
x43

asked: Jan 21, 2010 at 10:21 PM

Seen: 1356 times

Last Updated: Jan 21, 2010 at 11:42 PM