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

 Author Message dhananjay.nagarkar Right there with Babe Group: General Forum Members Points: 736 Visits: 185 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 --------------------------ThanksDJ Sean Lange SSC Guru Group: General Forum Members Points: 150057 Visits: 18581 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 --------------------------ThanksDJCan 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.Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Understanding and Using APPLY (Part 1)Understanding and Using APPLY (Part 2) Luis Cazares SSC Guru Group: General Forum Members Points: 96378 Visits: 21215 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 --------------------------ThanksDJYou'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 dhananjay.nagarkar Right there with Babe Group: General Forum Members Points: 736 Visits: 185 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.ThanksDJ dhananjay.nagarkar Right there with Babe Group: General Forum Members Points: 736 Visits: 185 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.ThanksDJ Luis Cazares SSC Guru Group: General Forum Members Points: 96378 Visits: 21215 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.ThanksDJSQL 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 dhananjay.nagarkar Right there with Babe Group: General Forum Members Points: 736 Visits: 185 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.thanksDJ Luis Cazares SSC Guru Group: General Forum Members Points: 96378 Visits: 21215 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 Sean Lange SSC Guru Group: General Forum Members Points: 150057 Visits: 18581 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.Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Understanding and Using APPLY (Part 1)Understanding and Using APPLY (Part 2) Sean Lange SSC Guru Group: General Forum Members Points: 150057 Visits: 18581 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.Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Understanding and Using APPLY (Part 1)Understanding and Using APPLY (Part 2)