?
Solved

DateDiff calculation and updating it into other field in original/parent table

Posted on 2009-07-14
4
Medium Priority
?
206 Views
Last Modified: 2012-05-07
I created a query that when a number is entered into it - the value will autopoulate and appear in the same field in another table.

SQL VIEW:
UPDATE [AUDIT ISSUES] SET [AUDIT ISSUES].[Days To Target Date] = DateDiff("d",[AUDIT ISSUES].[Date Opened],[AUDIT ISSUES].[Date Closed]);

However, I need my field named "Days To Target Date" to calculate the difference between two fields "Date Opened" and "Date Closed" but these are not appearing on my datasheet view of the query (I am attaching a screen shot)

I was able to create a separate query that was able to calculate the date difference
SQL VIEW:
SELECT [Audit Issues].[ID], [Audit Issues].[Severity], [Audit Issues].[Auto Report Date], [Audit Issues].[Date Opened], DateDiff("d",[Date Opened],[Auto Report Date]) AS [Days To Target Date]
FROM [Audit Issues];

So maybe it would be easier if I update that field "Days to Target Date" into my table - or combine the two somehow?

If somoene could help me with the formatting of what I should write in the SQL view to combine all of this -- that is, to have it find the date difference and autopopulate my original "Audit Issues" table that would be greatly appreciated.

THANK YOU.

screenshot-3.JPG
0
Comment
Question by:MaeMcG
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24851887
You original UPDATE statement should not have resulted in any results shown on screen, as you are not doing a select.  It should have prompted that you are about to change x number of records and you can proceed yes or no.  Verify the column names as in the update you use [Date Closed] but in the select where it "worked" you used [Auto Report Date].
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24851911
And since you are using my solution for calculating the days until target, I would ask you please close this question - http:/Q_24566883.html.
0
 

Author Comment

by:MaeMcG
ID: 24853908
I'm still confused?
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1500 total points
ID: 24854117
On which question?  On the one I posted, it seems that you are using my suggestion here to calculate the days to target as DATEDIFF("d", start_date, target_date).

For this question, since the calculation requires valid dates or date columns, you have to ensure that what you are passing to the function is correct.  My initial post was pointing out to you when running an UPDATE table you are making changes to underlying table data and not doing a SELECT; therefore, you should not see any results.  You should be seeing a prompt telling you the number of rows that were/will be impacted.

Additionally, you have this:
UPDATE [AUDIT ISSUES]
SET [AUDIT ISSUES].[Days To Target Date] = [b]DateDiff("d",[AUDIT ISSUES].[Date Opened],[AUDIT ISSUES].[Date Closed])[/b];

But in this query, you have this:
SELECT [Audit Issues].[ID], [Audit Issues].[Severity], [Audit Issues].[Auto Report Date], [Audit Issues].[Date Opened]
, [b]DateDiff("d",[Date Opened],[Auto Report Date]) AS [Days To Target Date][/b]
FROM [Audit Issues];

Note the differences of the text between [b] or in bold if the style is applied correctly.  If the one with [Date Closed] is not working, check that column is correct.  Also check if all your rows actual have dates for opened and closed dates.
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question