Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
tc
Starting Member
11 Posts |
Posted - 2006-02-07 : 12:09:57
|
| I have the following code I'm using in Microsoft Access to find out what crew is working for a specific date. I want to create a UDF that does the same thing, but I'm not sure where to start. Instead of an array should I be using a table?Const ShiftCycleStart As Date = "1/6/1997" 'known starting date for shift rotationConst AnodeCycleStart As Date = "7/23/2003"Function Get_Shift12hr(SDate As Date, DayOrNight As Integer) As String' This function calculates the crew that is working for a given day (SDate) and Shift (DayOrNight)' Crew rotation works on a 28 day cycle' DayOrNight should be 0 for dayshift and 1 for nightshift Dim Cycle12Hour(28, 1) As String Dim DayInCycle As Integer 'populate the array for the shiftcycle Cycle12Hour(1, 0) = "H": Cycle12Hour(1, 1) = "J" Cycle12Hour(2, 0) = "H": Cycle12Hour(2, 1) = "I" Cycle12Hour(3, 0) = "H": Cycle12Hour(3, 1) = "I" Cycle12Hour(4, 0) = "H": Cycle12Hour(4, 1) = "I" Cycle12Hour(5, 0) = "J": Cycle12Hour(5, 1) = "K" Cycle12Hour(6, 0) = "J": Cycle12Hour(6, 1) = "K" Cycle12Hour(7, 0) = "J": Cycle12Hour(7, 1) = "K" Cycle12Hour(8, 0) = "I": Cycle12Hour(8, 1) = "K" Cycle12Hour(9, 0) = "I": Cycle12Hour(9, 1) = "J" Cycle12Hour(10, 0) = "I": Cycle12Hour(10, 1) = "J" Cycle12Hour(11, 0) = "I": Cycle12Hour(11, 1) = "J" Cycle12Hour(12, 0) = "K": Cycle12Hour(12, 1) = "H" Cycle12Hour(13, 0) = "K": Cycle12Hour(13, 1) = "H" Cycle12Hour(14, 0) = "K": Cycle12Hour(14, 1) = "H" Cycle12Hour(15, 0) = "J": Cycle12Hour(15, 1) = "H" Cycle12Hour(16, 0) = "J": Cycle12Hour(16, 1) = "K" Cycle12Hour(17, 0) = "J": Cycle12Hour(17, 1) = "K" Cycle12Hour(18, 0) = "J": Cycle12Hour(18, 1) = "K" Cycle12Hour(19, 0) = "H": Cycle12Hour(19, 1) = "I" Cycle12Hour(20, 0) = "H": Cycle12Hour(20, 1) = "I" Cycle12Hour(21, 0) = "H": Cycle12Hour(21, 1) = "I" Cycle12Hour(22, 0) = "K": Cycle12Hour(22, 1) = "I" Cycle12Hour(23, 0) = "K": Cycle12Hour(23, 1) = "H" Cycle12Hour(24, 0) = "K": Cycle12Hour(24, 1) = "H" Cycle12Hour(25, 0) = "K": Cycle12Hour(25, 1) = "H" Cycle12Hour(26, 0) = "I": Cycle12Hour(26, 1) = "J" Cycle12Hour(27, 0) = "I": Cycle12Hour(27, 1) = "J" Cycle12Hour(28, 0) = "I": Cycle12Hour(28, 1) = "J" DayInCycle = ((SDate - ShiftCycleStart) Mod 28) + 1 Get_Shift12hr = Cycle12Hour(DayInCycle, DayOrNight)End Function |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-02-07 : 13:46:19
|
U can use a temp table as an arrayThe table can be populated, more or less like a normal tableCreate table #Cycle12Hour( DayInCycle int, DayOrNight int, Shift12hr varchar(1) )Insert Into #Cycle12HourSelect 1,0,'H' Union AllSelect 2,0,'H' Union AllSelect 3,0,'H' Union AllSelect 4,0,'H' Union AllSelect 5,0,'J' Union All........Select 6,1,'K' Union AllSelect 7,1,'K' Union All...Select 28,1,'J' Then instead of Get_Shift12hr = Cycle12Hour(DayInCycle, DayOrNight)Usesomething similar to the following:-- @DayInCycle and @DayOrNight are the parameters passed to the functionDeclare @Get_Shift12hr varchar(1)Set @Get_Shift12hr = (Select Shift12hr from #Cycle12Hour where DayInCycle = @DayInCycle and DayOrNight = @DayOrNight) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-02-08 : 03:38:37
|
| >>Set @Get_Shift12hr = (Select Shift12hr from #Cycle12Hour where >> DayInCycle = @DayInCycle and DayOrNight = @DayOrNight)you will get error if the subquery returns more than one dataThe safer method is to useSelect @Get_Shift12hr = Shift12hr from #Cycle12Hour where DayInCycle = @DayInCycle and DayOrNight = @DayOrNightMadhivananFailing to plan is Planning to fail |
 |
|
|
tc
Starting Member
11 Posts |
Posted - 2006-02-08 : 16:32:02
|
| Sorry for my ignorance, but I'm new to all of this. Do I need to create a function that creates the temp table and then references that table? |
 |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2006-02-08 : 19:09:40
|
| I answered with the assumption thet need to convert static array used in Access is to be used in a UDF of SQL server 2000.If I'm not correctcould you please tell what do you want to do. Not the logic of the function (which I can see). Also tell, why do u want to create a function?Ur concern is creating a function or making use of an array? or anything else? |
 |
|
|
tc
Starting Member
11 Posts |
Posted - 2006-02-09 : 08:30:47
|
| Thanks for your reply. Originally, my table was in Access, now the data has been moved to a SQL server. I now have the table with the following format:Sample_Date Pot_No Sample_Value01/01/2006 7:00:00AM 1 2.301/01/2006 7:00:00AM 2 3.601/01/2006 7:00:00AM 3 4.501/01/2006 7:00:00PM 1 2.701/01/2006 7:00:00PM 2 6.301/01/2006 7:00:00PM 3 9.2So based on this data, I need to find out what crew was working for each shift. I don't have any experience with UDF's, I just assumed that would be the best way to go, am I going about this wrong? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-02-09 : 08:33:59
|
| can you explain what is this table ? How is it related to the ShiftCycle ?----------------------------------'KH'everything that has a beginning has an end |
 |
|
|
tc
Starting Member
11 Posts |
Posted - 2006-02-09 : 10:34:20
|
| Each day consists of two shifts, based on the "Sample_Date" (if ends in "AM" it's day shift, "PM" it's night shift) and the array originally mentioned, I need to determine what crew was working for each shift. I apologize for not explaining myself very well and hope this helps. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-02-09 : 20:32:57
|
| Sorry... still a bit lost on what you want.>> I need to determine what crew was working for each shiftWhere will the crew information comes from ? the ShiftCycle only contain the CycleDay, AM / PM Shift and 12hourshift ?Can you post your tables DDL, some sample data and expected result also ?----------------------------------'KH'everything that has a beginning has an end |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
tc
Starting Member
11 Posts |
Posted - 2006-02-10 : 15:38:43
|
| I found a way around that seems pretty clumsy, feedback would be appreciated.First, I set up this function:CREATE FUNCTION dbo.GetCycle (@SDate datetime)RETURNS intASBEGIN RETURN ((DateDiff(Day,'01/06/1997',dbo.DatePart(@SDate))) % 28) + 1ENDThen I run a select statement on the following table:SDateTime--------------Pot_No---Shift---Aes2006-01-01 00:00:00.000 1 DAY 12006-01-01 00:00:00.000 2 DAY 32006-01-01 00:00:00.000 3 DAY 22006-01-01 00:00:00.000 1 NIGHT 62006-01-01 00:00:00.000 2 NIGHT 12006-01-01 00:00:00.000 3 NIGHT 3SQL Statement:SELECT dbo.DatePart(SDateTime) as Day, Pot_No, Shift, Aes, (CASE WHEN dbo.GetCycle(SDateTime) < 5 AND Shift = 'DAY' THEN 'H' WHEN dbo.GetCycle(SDateTime) > 4 and dbo.GetCycle(SDateTime) < 8 AND Shift = 'DAY' THEN 'J' WHEN dbo.GetCycle(SDateTime) > 7 and dbo.GetCycle(SDateTime) < 12 AND Shift = 'DAY' THEN 'I' WHEN dbo.GetCycle(SDateTime) > 11 and dbo.GetCycle(SDateTime) < 15 AND Shift = 'DAY' THEN 'K' WHEN dbo.GetCycle(SDateTime) > 14 and dbo.GetCycle(SDateTime) < 19 AND Shift = 'DAY' THEN 'J' WHEN dbo.GetCycle(SDateTime) > 18 and dbo.GetCycle(SDateTime) < 22 AND Shift = 'DAY' THEN 'H' WHEN dbo.GetCycle(SDateTime) > 21 and dbo.GetCycle(SDateTime) < 26 AND Shift = 'DAY' THEN 'K' WHEN dbo.GetCycle(SDateTime) > 25 AND Shift = 'DAY' THEN 'I' WHEN dbo.GetCycle(SDateTime) =1 AND Shift = 'NIGHT' THEN 'J' WHEN dbo.GetCycle(SDateTime) > 1 and dbo.GetCycle(SDateTime) < 5 AND Shift = 'NIGHT' THEN 'I' WHEN dbo.GetCycle(SDateTime) > 4 and dbo.GetCycle(SDateTime) < 9 AND Shift = 'NIGHT' THEN 'K' WHEN dbo.GetCycle(SDateTime) > 8 and dbo.GetCycle(SDateTime) < 12 AND Shift = 'NIGHT' THEN 'J' WHEN dbo.GetCycle(SDateTime) > 11 and dbo.GetCycle(SDateTime) < 16 AND Shift = 'NIGHT' THEN 'H' WHEN dbo.GetCycle(SDateTime) > 15 and dbo.GetCycle(SDateTime) < 19 AND Shift = 'NIGHT' THEN 'K' WHEN dbo.GetCycle(SDateTime) > 18 and dbo.GetCycle(SDateTime) < 23 AND Shift = 'NIGHT' THEN 'I' WHEN dbo.GetCycle(SDateTime) > 2 and dbo.GetCycle(SDateTime) < 26 AND Shift = 'NIGHT' THEN 'H' WHEN dbo.GetCycle(SDateTime) > 25 AND Shift = 'NIGHT' THEN 'J' Else 'Z' End) As CrewFROM dbo.tblPotHistI then get the desired output:Day----------Pot_No-----Shift--Aes-----Crew01/01/2006 1 DAY 1 J01/01/2006 2 DAY 3 J01/01/2006 3 DAY 2 J01/01/2006 1 NIGHT 6 K01/01/2006 2 NIGHT 1 K01/01/2006 3 NIGHT 3 K |
 |
|
|
|
|
|
|
|