The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.

The below function is giving me the hours difference what I wanted, but today it is giving us the below error: 
Msg 535, Level 16, State 0, Line 1
The datediff function resulted in an overflow. The number of dateparts separating two date/time instances is too large. Try to use datediff with a less precise datepart.
Please Help..
ALTER FUNCTION [dbo].[GetHoursExcludingWeekdays](@StartDate datetime2,@EndDate datetime2)
returns decimal(12,3)
as
begin
    if datepart(weekday,@StartDate) = 1
        set @StartDate = dateadd(day,datediff(day,0,@StartDate),1)
    if datepart(weekday,@StartDate) = 7
        set @StartDate = dateadd(day,datediff(day,0,@StartDate),2)
    -- if @EndDate happens on the weekend, set to previous Saturday 12AM
    -- to count all of Friday's hours
    if datepart(weekday,@EndDate) = 1
        set @EndDate = dateadd(day,datediff(day,0,@EndDate),-2)
    if datepart(weekday,@EndDate) = 7
        set @EndDate = dateadd(day,datediff(day,0,@EndDate),-1)
    declare @return decimal(12,3)
    set @return = ((datediff(second,@StartDate,@EndDate)/60.0/60.0) - (datediff(week,@StartDate,@EndDate)*48))
    return @return
end
ReportingServices

You'll get this error if the difference between the start and end date is greater that about 68 years due to the "second" DATEDIFF specification.  Perhaps the dates are greater than the expected range due to a data quality issue. 
Taking the advice from the error message, you could use minutes instead of seconds like the example below the version below.  This could still result in the error of the difference is greater than a couple of hundred years, though.  You might consider
validating the dates and returning NULL if outside expected limits.
ALTER FUNCTION [dbo].[GetHoursExcludingWeekdays](@StartDate datetime2,@EndDate datetime2)
returns decimal(12,3)
as
begin
if datepart(weekday,@StartDate) = 1
set @StartDate = dateadd(day,datediff(day,0,@StartDate),1)
if datepart(weekday,@StartDate) = 7
set @StartDate = dateadd(day,datediff(day,0,@StartDate),2)
-- if @EndDate happens on the weekend, set to previous Saturday 12AM
-- to count all of Friday's hours
if datepart(weekday,@EndDate) = 1
set @EndDate = dateadd(day,datediff(day,0,@EndDate),-2)
if datepart(weekday,@EndDate) = 7
set @EndDate = dateadd(day,datediff(day,0,@EndDate),-1)
declare @return decimal(12,3)
set @return = ((datediff(minute,@StartDate,@EndDate)/60.0) - (datediff(week,@StartDate,@EndDate)*48))
return @return
end
GO
Dan Guzman, SQL Server MVP, http://www.dbdelta.com

Similar Messages

  • Get the Stored Function Results

    If there is DML in my Stored Function ,how to Using View Objects to Select Stored Function Results?
    I want to get the Stored Function Results in my jsp.
    please give me the example jsp.Thanks!

    There's an article on my "Dive into BC4J" weblog that describes this.
    http://radio.weblogs.com/0118231/stories/2003/02/06/usingViewObjectsToSelectStoredFunctionResults.html

  • The InitCVIRTE function is not listed in the NIDAQ function reference online help? Why? and where can I find a description of this function?

    the InitCVIRTE function is not listed in the NIDAQ function reference online help? Why? and what does she do?and where can I find a description of this function? Can i use this function with visualc++ 6.0?

    The InitCVIRTE function is in the CVI run time engine (cvirte.dll)..not part of NI-DAQ.
    Applications written or using CVI may call this function..
    How are you running into this ?
    From the CVI help...
    This function performs initialization of the CVI Run-Time Engine. It is needed only in executables or DLLs that are linked using an external compiler. Otherwise, it is harmless.
    It should be called in your main, WinMain, or DllMain, function. The parameter values you should pass depend on which of these three functions you are calling InitCVIRTE from. The following examples show how to use InitCVIRTE in each case.
    If you are using main, your code should be as follows.
    int main (int argc, char *argv[])
    if (InitCVIRTE (0, argv, 0) == 0)
    return -1; /* out of memory */
    /* your other code */
    return 0;
    If you are using WinMain, your code should be as follows.
    int __stdcall WinMain (HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpszCmdLine,
    int nCmdShow)
    if (InitCVIRTE (hInstance, 0, 0) == 0)
    return -1; /* out of memory */
    /* your other code */
    return 0;
    If you are creating a DLL, you must call InitCVIRTE and CloseCVIRTE in your DllMain function, as in the following.
    int __stdcall DllMain (void *hinstDLL, int fdwReason,
    void *lpvReserved)
    if (fdwReason == DLL_PROCESS_ATTACH)
    if (InitCVIRTE (hinstDLL, 0, 0) == 0)
    return 0; /* out of memory */
    /* your other ATTACH code */
    else if (fdwReason == DLL_PROCESS_DETACH)
    /* your other DETACH code */
    CloseCVIRTE ();
    return 1;
    NOTE: The prototype for InitCVIRTE is in cvirte.h, not
    utility.h.
    NOTE: In CVI 4.0.1, this function was expanded from one to
    three parameters. Executables and DLLs that were
    created using the one-parameter version of the function
    will continue to work properly.
    /*-------------------- Prototype ---------------------*/
    int InitCVIRTE (void *HInstance, char *Argv[], void *Reserved);
    Nandan Dharwadker
    Staff Software Engineer
    Measurement Studio Hardware Team

  • The Find function (Ctrl+F), mis-finds the desired simple text in Acrobat Standard, but works fine in Reader

    Adobe Acrobat X 10.1.13, Win 7 64bit, 
    The Find function (Ctrl+F), mis-finds the desired simple text. 
    This is not a scanned document.  When I search for some word that DOES exist in the document, Acrobat will highlight text that does not match at all and which does NOT contain the word.  If I select the actual word, copy and past the text, I get the selected text accurately, but when I paste the same selected text into the Find box to search for it, Acrobat still mis-finds. 
    HOWEVER, I can open the very same .PDF file in Acrobat Reader X and do not have the same problem - Find works correctly in Reader with the same .PDF. 
    What is going on?!  Both Acrobat standard, and Reader report that they are up to date.   Please help.

    Can you try rasterizing (converting the document to tiff/bmp) and then importing those image files (tiff/bmp) back in Acrobat.
    This would allow document to be re-OCRed and you will be able to search text in it. You can use lossless export/import settings to preserve the quality of ducment.
    For exporting the file:
    1: Go to File > Save As Other > Image > Tiff.
    2: Click on Settings button in the Save As Dialog and set the file settings as ZIP for Monochrome, Grayscale and Color.
    Now for importing the files back:
    1: Press Ctrl + K
    2: In the Preferences dialog, select Convert To PDF option in Categories.
    3: Choose tiff or bmp (as per the format used while exporting) and un-check Scan Optimization and OCR.
    4: Set Color and Grayscale to Zip and Monochrome to CCITT G4.
    5: Save the settings.
    6: Now import all the pages in Acrobat.
    7: You can use Combine Files feature to merge all the image files.

  • In the new iOS-7 Safari, has the "reader" function been changed to eliminate the option to modify font size (and hence to modify the number of words per line), or is it just that I can't find how to do that?

    In the new iOS-7 Safari, has the "reader" function been changed to eliminate the option to modify font size (and hence to modify the number of words per line), or is it just that I can't find how to do that?

    iOS 7
    Seperate text size modification is no longer available in Safari Reafer.
    Use Settings.
    Settings >General > Text Size

  • Why is the scroll function not working after accessing the voice feature in settings?

    Why is the scroll function not working after accessing the voice feature in settings?

    For some prerecorded audio files the Transpose slider will not work reliably. I found no clear pattern, when it does and when not.
    If all fails, create a loop from the audio file. Select the region in the track and use the command "File: Add region to Loop library".
    Set the current key and tempo, name the loop, and save the loop.
    Now add it from the loop browser to a new track and delete your old track. The search field in the Loop browser will help you to find the loop quickly.
    You will be able to transpose the sample as described above, like a regular loop.
    For example - a audio file turned into a loop and transposed:

  • Why do i constantly get cut off on calls.  I've had this problem with my 3GS, my 4S and now my iPhone 5.  When the core functionality is Phone, i.e. the ability to talk, why can't Apple make the iPhone function as a Phone?  is there an App for that !!

    why do i constantly get cut off on calls.  I've had this problem with my 3GS, my 4S and now my iPhone 5.  When the core functionality is Phone, i.e. the ability to talk, why can't Apple make the iPhone function as a Phone?  is there an App for that !!

    Well, since you've had the same problem, with three different phones, that all use different size sim cards, it seems pretty obvious the problem is with your carrier/coverage & nothing to do with your phone or sim card.

  • [nQSError: 46104] The current operation was about to overflow the stack.

    Hi BI,
    I have some reports that return the following error:
    [nQSError: 43113] Message returned from OBIS. [nQSError: 46104] The current operation was about to overflow the stack. Please simplify or discontinue from the current operation. (HY000)
    Does anyone know what could be the causes? Or possible solutions?
    Thanks in advance.

    Hi,
    If the stack overflow is occurring there would be a coredump file created. Evaluate the coredump file.
    If the issue is only while running the report then increasing SERVER_THREAD_STACK_SIZE in NQSConfig.INI will work.
    If the issue causing during start up of services then OBI_BACKGROUND_STACK_SIZE needs to be set in opmn.xml.
    Let me know if this helped.
    Regards,
    Jay

  • Filter function to one of the  arraycollections the same function gets applied to all the other  arraycollecti

    If I instantiate 3 arraycollections and point them to the
    same object with the = operator, then if I apply a filter function
    to one of the arraycollections the same filter function gets
    applied to all the other arraycollections when I refresh the
    arraycollection with the filter. How can I get round this?

    "nikos101" <[email protected]> wrote in
    message
    news:gb8h30$dt9$[email protected]..
    > If I instantiate 3 arraycollections and point them to
    the same object with
    > the
    > = operator, then if I apply a filter function to one of
    the
    > arraycollections
    > the same function gets applied to all the other
    arraycollections. How can
    > I
    > get round this?
    private var ac1:ArrayCollection= new ArrayCollection();
    private var ac2:ArrayCollection= new ArrayCollection();
    private var ac3:ArrayCollection= new ArrayCollection();
    private function
    initArrayCollections(sourceArray:Array):void{
    ac1.source= sourceArray;
    ac2.source= sourceArray;
    ac3.source= sourceArray;
    ac1.filterFunction=myFilter;
    ac1.refresh();

  • The finder window for an external USB hard disk suddenly started separating files by time (today, yesterday, etc.) and stopped allowing me to change column width, order, etc. Also the header bar shifted to flat white from gray.

    The finder window for an external USB hard disk suddenly started separating files by time (today, yesterday, etc.) and stopped allowing me to change column width, order, etc. Also the header bar shifted to flat white from gray.
    All was normal until the icon for the disk stopped showing up on the desktop when I would plug it in. I rebooted the computer, and the disk icon now shows up but this new look to the finder window started as a result. The hard disk does not have this problem when I use it on another Mac running Snow leopard. Also, two other external hard disks that I use on this computer running lion 7.4 still have the normal header bars with columns that can be reordered, etc.
    I have tried looking in preferences and other places but have not seen this discussed. 

    Well duh - I finally figured out that somehow in the View options for the hard drive, the "arrange by" option had been changed to "by date" from "none". Not sure how that happened, I had never used the "view options" menu tem before. Changing "arrange by" back to "none" gets the gray column headers and the collapsible folder icons back. 

  • In base component when user change the serial number we need to set date time in Extra attribute filed

    HI Team,
    In ibase component when user change the serial number we need to set date time in Extra attribute filed..if we click edit ,User change serial number after save we need to set date time in  Extra Attribute 4 field .
    Component - PRDGENSET
    In this component there is no attributes,so where we need to implement and how to implement please help.
    Thanks
    kalpana

    Hi Abi,
    Thanks for your reply..see i debugged..i have put break point in method SET_S_STRUCT method if press enter in serial number ,debuggeris triggered..in SET_S_STRUCT method value getting serial number..so how to write the logic here...
    In GET_S_STRUCT method iam getting all attributes  but serial number is not filled..So could you please help here what to do..
    in set_S_STRCUT method value iam gettin serial number...in collection wrapper iam geting zrealtions if i open attributes_ref -if i pass Attribute value in run time its getting displayed.so how to write the logic based on serial  number please help some coding.
    Thanks
    kalpana

  • I wanted to know how do you calculate the number of days between two dates

    i wanted to know how do you calculate the number of days between two dates in java ? i get both the dates from the database. i guess there are many issues like leap year and Febuary having diff no of months ..etc.

    thanks..
    I solve my problem as
    public class MyExample {
        public static void main(String a[]) {
            String stdate = "2009-03-01";
            java.sql.Date currentDate = new java.sql.Date(System.currentTimeMillis());
            java.sql.Date preDate = java.sql.Date.valueOf(stdate);
            System.out.println(currentDate);
            System.out.println(preDate);
    //        int dateCom = preDate.compareTo(currentDate);
    //        System.out.println(dateCom);
            long diff = currentDate.getTime() - preDate.getTime();
            int days = (int) Math.floor(diff / (24 * 60 * 60 * 1000));
             System.out.println(days);
    }

  • When I am trying to download (after getting the email that it is ready to download adobe acrobat pro) it says this - 413  Header Length too Large  Any suggestions??

    When I am trying to download (after getting the email that it is ready to download adobe acrobat pro) it says this - 413  Header Length too Large  Any suggestions??Ado

    Moving this discussion to the Acrobat Installation & Update Issues forum.
    Kltjlt6458 to resolve the current error clear your current web browsers cache and cookies.  If you continue to receive the same error then please utilize a different web browser.

  • I bught a new Time Capsule 2Tb. I connected it to my internet router. I manege to set it up as base station but when i try to use it with time machine I always get the message "Check your IP addres"? What am I doing wrong? On my computer I have OSX 10.7.5

    I bought a new Time Capsule 2Tb. I connected it to my internet router. I manage to set it up as base station but when i try to use it with time machine I always get the message "Check your IP addres"? What am I doing wrong? On my computer I have OSX 10.7.5

    What IP address does the TC have and what IP does the computer have?
    We need more info as we really have no idea what your network looks like.
    Modem router, model, type of broadband?
    TC setup as bridge or router?
    Is the computer getting internet via the TC?
    Are you using wireless or ethernet?
    Have you set IPv6 correctly to local link in the computer on whichever network client is doing the connection?
    Did this work the first time for backup and has now failed? If so simply reboot the TC. It is a constant bug that the TC is lost to the network.

  • Every time l open filters and try to use the oil painting filter photoshop closes down !!

    Every time l open filters and try to use the oil painting filter photoshop closes down !!

    Update your graphics driver. The oil paint filter uses GPU acceleration.
    Mylenium

Maybe you are looking for

  • *** SOLUTIONS POST HERE - To the iOS 5 & iCloud Apple ID Fiasco...

    PLEASE POST ALL YOUR SOLUTIONS HERE There are so many topics for this now, it's mind numbing to find some answers. I'll do my best to post some realizations that I've had, and methods to work around what Apple hasn't properly explained yet. Issue: E-

  • My mac wont recognize any of my devices

    I have recently updated my phone from a 4 to iphone5 and was in the process of trying to transfer pics from old phone to mac. Now my mac will not recognize any of my devices finder, image capture, iphoto....it does however show up in itunes when plug

  • Move button to the next page with his function.

    Hello, is the any way to move a button to the another page with his functions? I can't creat a new button, because I need all the function that contains the button original. I use it now, but it is not ok for to change te page because it has not the

  • Alternative for SO_NEW_DOCUMENT_SEND_API1 in ECC 5.0?

    Hi Would like to know what is the alternative method to implement the logic to send email in ECC 5.0 .  SO_NEW_DOCUMENT_SEND_API1  seems to be obsolete . Primarily I would like to know the difference between the following 2 function module. SO_OBJECT

  • Trouble with QT PRO

    I need help converting WMV files. I know that it's suppose to be possible with PRO. What do I do? Need to put them on my iPod.