Question : Sanity Check on UDF Modification

I've modified my UDF to add extra rates for the weekend daya and night. The rates should now be applied for day/night then same as the weekday rates.

Does this look correct:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
USE [EnergySuiteDB]
GO
/****** Object:  UserDefinedFunction [dbo].[udf_getrates_3]    Script Date: 07/21/2010 15:37:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--***********************FUNCTION***********************--
ALTER FUNCTION [dbo].[udf_getrates_3] (@date datetime,@util varchar(50))
RETURNS @tbl table (chargerate MONEY, levy MONEY, caprate MONEY,ChargeCap float, StandingDayCharge money)
AS
BEGIN
  insert @tbl 
  select Top 1 --EffectiveStartDate, EffectiveEndDate, 
               CASE WHEN left(DATENAME(DW, @date),1) = 'S' and isnull(weekenddaycost,0) <> 0
                    THEN
                     CASE WHEN datepart(hour,@date) between datepart(hour,weekenddaystart) and datepart(hour,weekenddayend)
                     THEN
                     weekenddaycost
                     ELSE
                     weekendnightcost
                     END
                    ELSE 
                         CASE
                         WHEN datepart(hour,@date) between datepart(hour,weekdaystart) and datepart(hour,weekdayend)
                         THEN weekdaycost
                         ELSE weeknightcost
                         END
                    END , climateChangeLevyRate, ((chargeablecapacity*capacityrate)),chargeablecapacity, isnull(StandingDayCharge,0)
  FROM EnergySuiteDB.dbo.Rates
  where UtilityName=@util 
  AND @date between EffectiveStartDate and EffectiveEndDate 
  Order By ID desc    -- get the "last" row that covers the requested date.

  if @@rowcount = 0
     insert @tbl 
     select Top 1 --EffectiveStartDate, EffectiveEndDate,
                  CASE WHEN left(DATENAME(DW, @date),1) = 'S' and isnull(weekenddaycost,0) <> 0
                       THEN
                     CASE WHEN datepart(hour,@date) between datepart(hour,weekenddaystart) and datepart(hour,weekenddayend)
                     THEN
                     weekenddaycost
                     ELSE
                     weekendnightcost
                     END
                    ELSE 
                         CASE
                         WHEN datepart(hour,@date) between datepart(hour,weekdaystart) and datepart(hour,weekdayend)
                         THEN weekdaycost
                         ELSE weeknightcost
                         END
                       END , climateChangeLevyRate, ((chargeablecapacity*capacityrate)),chargeablecapacity, isnull(StandingDayCharge,0)
     FROM EnergySuiteDB.dbo.Rates
     where UtilityName=@util 
     order by EffectiveEndDate desc   -- highest enddate belongs to "row 1" the all covering / default rate.
  return 
END

Answer : Sanity Check on UDF Modification

Well, I do think it needs the extra checking in there...

So, have a look at :
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
ALTER FUNCTION [dbo].[udf_getrates_3] (@date datetime,@util varchar(50))
RETURNS @tbl table (chargerate MONEY, levy MONEY, caprate MONEY,ChargeCap float, StandingDayCharge money)
AS
BEGIN


  declare @dw char(1)
  set @dw = left(DATENAME(DW, @date),1)

  declare @hr int
  set @hr = datepart(hour,@date)

  insert @tbl 
  select Top 1 --EffectiveStartDate, EffectiveEndDate, 
         CASE WHEN @dw = 'S' and (   (@hr between datepart(hour,weekenddaystart) and datepart(hour,weekenddayend) and isnull(weekenddaycost,0) <> 0)
                                  or (@hr NOT between datepart(hour,weekenddaystart) and datepart(hour,weekenddayend) and isnull(weekendnightcost,0) <> 0))
              THEN
                     CASE WHEN @hr between datepart(hour,weekenddaystart) and datepart(hour,weekenddayend)
                          THEN weekenddaycost
                          ELSE weekendnightcost
                     END
              ELSE 
                     CASE WHEN @hr between datepart(hour,weekdaystart) and datepart(hour,weekdayend)
                          THEN weekdaycost
                          ELSE weeknightcost
                     END
         END , climateChangeLevyRate, ((chargeablecapacity*capacityrate)),chargeablecapacity, isnull(StandingDayCharge,0)
  FROM   EnergySuiteDB.dbo.Rates
  where  UtilityName=@util 
  AND @date between EffectiveStartDate and EffectiveEndDate 
  Order By ID desc    -- get the "last" row that covers the requested date.

  if @@rowcount = 0
     insert @tbl 
     select Top 1 --EffectiveStartDate, EffectiveEndDate, 
         CASE WHEN @dw = 'S' and (   (@hr between datepart(hour,weekenddaystart) and datepart(hour,weekenddayend) and isnull(weekenddaycost,0) <> 0)
                                  or (@hr NOT between datepart(hour,weekenddaystart) and datepart(hour,weekenddayend) and isnull(weekendnightcost,0) <> 0))
              THEN
                     CASE WHEN @hr between datepart(hour,weekenddaystart) and datepart(hour,weekenddayend)
                          THEN weekenddaycost
                          ELSE weekendnightcost
                     END
              ELSE 
                     CASE WHEN @hr between datepart(hour,weekdaystart) and datepart(hour,weekdayend)
                          THEN weekdaycost
                          ELSE weeknightcost
                     END
         END , climateChangeLevyRate, ((chargeablecapacity*capacityrate)),chargeablecapacity, isnull(StandingDayCharge,0)
     FROM EnergySuiteDB.dbo.Rates
     where UtilityName=@util 
     order by EffectiveEndDate desc   -- highest enddate belongs to "row 1" the all covering / default rate.
  return 
END
Random Solutions  
 
programming4us programming4us