CREATE FUNCTION DBO.RecurringPatternGetWareRuleValue (@AWareNumber INT, @ADateTime DATETIME, @ARuleType INT)  
RETURNS VARCHAR (10) AS  
BEGIN 

  DECLARE @Return VARCHAR(10)

  SELECT DISTINCT @Return = ISNULL(RecurringRule.RuleValue, '') FROM RecurringPatternSpecified WITH (NOLOCK) 
  INNER JOIN RecurringPatternWareXTab WITH (NOLOCK) ON RecurringPatternWareXTab.FK_RecurringPatternID = RecurringPatternSpecified.FK_RecurringPatternID
  INNER JOIN RecurringRule WITH (NOLOCK) ON RecurringPatternSpecified.FK_RecurringPatternID = RecurringRule.FK_RecurringPatternID
  WHERE RecurringPatternWareXTab.FK_WareNumber = @AWareNumber
  AND RecurringPatternSpecified.StartDateTime <= @ADateTime AND RecurringPatternSpecified.EndDateTime >= @ADateTime AND RecurringRule.RuleType = @ARuleType

  RETURN @Return

END

