Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
dhananjay.nagarkar
dhananjay.nagarkar
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 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

--------------------------
Thanks
DJ
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16582 Visits: 17024
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 Moden's 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
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8552 Visits: 18142
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
dhananjay.nagarkar
dhananjay.nagarkar
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 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.

Thanks
DJ
dhananjay.nagarkar
dhananjay.nagarkar
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 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.

Thanks
DJ
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8552 Visits: 18142
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
dhananjay.nagarkar
dhananjay.nagarkar
SSC Journeyman
SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)SSC Journeyman (90 reputation)

Group: General Forum Members
Points: 90 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.

thanks
DJ
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8552 Visits: 18142
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
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16582 Visits: 17024
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 Moden's 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
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16582 Visits: 17024
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 Moden's 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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search