Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8130987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:56:01+00:00 2026-06-06T08:56:01+00:00

I made a user-define function for business hours calculation. This is my UDF. CREATE

  • 0

I made a user-define function for business hours calculation.

This is my UDF.

CREATE FUNCTION  fn_GetBusinessHour (@date datetime, @addHours int)
RETURNS datetime
AS
BEGIN 
    DECLARE @CalcuatedDate datetime;
    DECLARE @addDayCount int, @addHourCount int, @addMinCount int;

    SET @addDayCount = @addHours / 8.5;
    SET @addHourCount = @addHours - (@addDayCount * 8.5);
    SET @addMinCount =  @addHours - (@addDayCount * 8.5) - @addHourCount;

    IF(@addDayCount != 0) 
        SET @CalcuatedDate = DATEADD(DD, @addDayCount, @date); 

    SET @CalcuatedDate = DATEADD(HH, @addHourCount, @CalcuatedDate);

    IF(@addMinCount != 0) 
        SET @CalcuatedDate = DATEADD(MM, @addMinCount, @CalcuatedDate); 

    RETURN @CalcuatedDate;
END

When I test using following statement,

SELECT dbo.fn_GetBusinessHour(GETDATE(), 40)

It shows proper result.

However, I use my function like this,

SELECT TicketID
     , DateTimeLogged --Type: Datetime
     , Priority       --Type: int
     , [dbo].[fn_GetBusinessHour](DateTimeLogged, Priority)
  FROM TicketHeader

the result shows only NULL value.

TicketID    DateTimeLogged  Priority    (No column name)
1   2011-07-04 11:26:19.510     30  NULL
2   2011-07-04 13:58:45.683     30  NULL
3   2011-07-05 10:09:16.923     10  NULL
4   2011-07-05 13:13:30.237     30  NULL
5   2011-07-05 16:50:34.033     20  NULL

I tried CONVERT because it worked when I give a value 40 but it also shows null values.

SELECT TicketID
         , DateTimeLogged --Type: Datetime
         , Priority       --Type: int
         , [dbo].[fn_GetBusinessHour](DateTimeLogged, CONVERT(int, Priority))
      FROM TicketHeader

How can I fix this to work my UDF?
Why this thing happen?
I cannot understand what is different between Priority and 40.

Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T08:56:02+00:00Added an answer on June 6, 2026 at 8:56 am

    For values of priority > 8.5, this seems to work fine for me:

    DECLARE @t TABLE(TicketID INT, DateTImeLogged DATETIME, Priority INT);
    
    INSERT @t SELECT 1,'20110704 11:26:19.510',30
    UNION ALL SELECT 2,'20110704 13:58:45.683',30
    UNION ALL SELECT 3,'20110705 10:09:16.923',10
    UNION ALL SELECT 4,'20110705 13:13:30.237',30
    UNION ALL SELECT 5,'20110705 16:50:34.033',20;
    
    SELECT TicketID
         , DateTimeLogged --Type: Datetime
         , Priority       --Type: int
         , [dbo].[fn_GetBusinessHour](DateTimeLogged, Priority)
      FROM @t;
    

    Yields:

    TicketID  DateTimeLogged           Priority  (No column name)
    --------  -----------------------  --------  -----------------------
    1         2011-07-04 11:26:19.510  30        2011-07-07 15:26:19.510
    2         2011-07-04 13:58:45.683  30        2011-07-07 17:58:45.683
    3         2011-07-05 10:09:16.923  10        2011-07-06 11:09:16.923
    4         2011-07-05 13:13:30.237  30        2011-07-08 17:13:30.237
    5         2011-07-05 16:50:34.033  20        2011-07-07 19:50:34.033
    

    If I add another row with a Priority < 8.5, e.g.:

    INSERT @t SELECT 6,'20110705 13:13:30.237',5;
    

    Then this row is added to the result:

    TicketID  DateTimeLogged           Priority  (No column name)
    --------  -----------------------  --------  -----------------------
    6         2011-07-05 13:13:30.237  5         NULL
    

    In other words, the function will output NULL if the function logic leaves @CalculatedDate unassigned, which will happen if @addDayCount = 0. In the function you say:

    IF(@addDayCount != 0) 
        SET @CalcuatedDate = DATEADD(DD, @addDayCount, @date); 
    

    Since @addDayCount is an INT, try this:

    DECLARE @addDayCount INT;
    SET @addDayCount = 5 / 8.5;
    SELECT @addDayCount;
    

    Result:

    0
    

    So because @CalculatedDate isn’t assigned a value initially, all of the following DATEADD operations are performing DATEADD(interval, number, NULL) which still yields NULL.

    So perhaps you need to use a different data type for the variables in the function…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made this function to verify a user's twitter credentials. Its running on two
Edit: With the answer given I made this function function grabclosestcolor($r, $g, $b){ $colors
According to my teacher, it's bad practice to write user-defined functions like this: int
I have made a function to give me the name of a user from
I'm creating a Tic tac toe game, in that after click made by user
so I made a simple user log in website, and I want to set
I have an application that needs to determine whether a user has made a
I am changing the background color of the cells when the user has made
I'm developing an iOS app, but i need to identify the user who made
i made an easy web user control by reading Scottgu articles BUT; my user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.