February 14, 2014 at 8:17 am
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
February 14, 2014 at 8:20 am
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/
February 14, 2014 at 8:27 am
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.
February 14, 2014 at 8:42 am
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
February 14, 2014 at 8:44 am
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
February 14, 2014 at 9:20 am
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 😉
February 14, 2014 at 9:24 am
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
February 14, 2014 at 10:18 am
What do you mean by O/P?
DATEDIFF will always return whole numbers.
February 14, 2014 at 10:23 am
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/
February 14, 2014 at 10:25 am
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/
February 14, 2014 at 10:57 am
My Bad , you are right DateDiff will always give whole number.
thanks
DJ
February 14, 2014 at 11:40 am
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/
February 14, 2014 at 12:17 pm
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