Help on Calculation of days between Start & End Date (Including BOTH the Start & End Dates)

  • Dear friends,

    I needed a urgent help on a critical task, for a field named "[ORC Position Duration (months)]"- I need to Calculate number of days from the "Target Start Date" through the "Temporary Assignment End Date" (including both the start date and end date) , -- Note I need to make sure that I include BOTH the Start Date and End Date in calculating the number of days between these two dates hence I wrote a code as below but I'm not sure will it work or not , if not please advice me the right code to accomplish this.

    ------ My code is as below---------------

    Select

    CONVERT(FLOAT,DATEDIFF ( DAY, [Pending].[Req# Target Start Date] ,[Pending].[Temporary Assignment End Date] ) + 1) AS [ORC Position Duration (months)]

    From dbo.pending

    --------------------------

    Thanks

    DJ

  • dhananjay.nagarkar (2/14/2014)


    Dear friends,

    I needed a urgent help on a critical task, for a field named "[ORC Position Duration (months)]"- I need to Calculate number of days from the "Target Start Date" through the "Temporary Assignment End Date" (including both the start date and end date) , -- Note I need to make sure that I include BOTH the Start Date and End Date in calculating the number of days between these two dates hence I wrote a code as below but I'm not sure will it work or not , if not please advice me the right code to accomplish this.

    ------ My code is as below---------------

    Select

    CONVERT(FLOAT,DATEDIFF ( DAY, [Pending].[Req# Target Start Date] ,[Pending].[Temporary Assignment End Date] ) + 1) AS [ORC Position Duration (months)]

    From dbo.pending

    --------------------------

    Thanks

    DJ

    Can you post the ddl and a few rows of sample data including the expected output? It is difficult to determine what you are trying to do here.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • dhananjay.nagarkar (2/14/2014)


    D I wrote a code as below but I'm not sure will it work or not , if not please advice me the right code to accomplish this.

    ------ My code is as below---------------

    Select

    CONVERT(FLOAT,DATEDIFF ( DAY, [Pending].[Req# Target Start Date] ,[Pending].[Temporary Assignment End Date] ) + 1) AS [ORC Position Duration (months)]

    From dbo.pending

    --------------------------

    Thanks

    DJ

    You're the best one to tell if the code works or not. I wouldn't CONVERT to float because it's an aproximate numeric and you are just dealing with integers (there could be a reason to convert it to float but it's not present in the code you posted).

    To make sure the code works, change the column values to well known values so you can make the calculation by yourself and compare it to the result of the query.

    Paraphrasing Nike: Just test it.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Hi Champion-

    I have to calculate the value for a field "[ORC Position Duration (months)]" using the following 2 fields from DataBase source- 1)Target Start Date , and 2) Temporary Assignment End Date.

    The 1st field Target Start Date in the table source is of the format smalldatetime example-2013-05-27 00:00:00.000, while the other field "Temporary Assignment End Date" is of the format nvarchar(255) and has values for example: "5-30-2013" - so I using these 2 fields I have to calculate the value for a field - "[ORC Position Duration (months)]" , and I need to make sure that I include both the Start Date and the End Date, and the calculation must result in a WHOLE number.

    Hope this explains more better for you to help me.

    Thanks

    DJ

  • Dear Journey Man,

    I have to calculate the value for a field "[ORC Position Duration (months)]" using the following 2 fields from DataBase source- 1)Target Start Date , and 2) Temporary Assignment End Date.

    The 1st field Target Start Date in the table source is of the format smalldatetime example-2013-05-27 00:00:00.000, while the other field "Temporary Assignment End Date" is of the format nvarchar(255) and has values for example: "5-30-2013" - so I using these 2 fields I have to calculate the value for a field - "[ORC Position Duration (months)]" , and I need to make sure that I include both the Start Date and the End Date, and the calculation must result in a WHOLE number.

    Hope this explains more better for you to help me.

    Thanks

    DJ

  • dhananjay.nagarkar (2/14/2014)


    Dear Journey Man,

    I have to calculate the value for a field "[ORC Position Duration (months)]" using the following 2 fields from DataBase source- 1)Target Start Date , and 2) Temporary Assignment End Date.

    The 1st field Target Start Date in the table source is of the format smalldatetime example-2013-05-27 00:00:00.000, while the other field "Temporary Assignment End Date" is of the format nvarchar(255) and has values for example: "5-30-2013" - so I using these 2 fields I have to calculate the value for a field - "[ORC Position Duration (months)]" , and I need to make sure that I include both the Start Date and the End Date, and the calculation must result in a WHOLE number.

    Hope this explains more better for you to help me.

    Thanks

    DJ

    SQL Server can't compare smalldatetime with nvarchar values. For datediff, it will convert your nvarchar value into a small datetime to be able to compare both values.

    With the dates and the formula you're posting this would be the result:

    SELECT DATEDIFF( DAY, '20130527', '20130530') + 1

    --Result 4

    Is this correct?

    Note: Our names are Sean and Luis 😉

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Hi Friend,

    it sounds correct since I'm using same code but I needed to know how to get the O/P of that code as a WHOLE NUMBER.

    thanks

    DJ

  • What do you mean by O/P?

    DATEDIFF will always return whole numbers.

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Luis Cazares (2/14/2014)


    What do you mean by O/P?

    DATEDIFF will always return whole numbers.

    I assume they meant output.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • This issue is incredibly to solve. You would have had an answer several hours ago if you would have read the part in my first post about posting ddl, sample data and desired output. Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • My Bad , you are right DateDiff will always give whole number.

    thanks

    DJ

  • So do you have this figured out now or do you still need some help?

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Yes thanks much 🙂

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply