Calculate "Expiration Date" Based on Date and Looked Up Value in MS SQL

I am creating an employee training tracking database. I have all of the training classes in a table along with the number of days said training is valid. (e.g. -[TrainingID],[TrainingName], [ExpirationDays])

I want to be able to have the expiration date automatically calculated when a user enters data into the training transactions table ([EmployeeID] foreign key, [TrainingID] foreign key, [TrainingDate] entered by user, [ExpirationDate] calculated based on [ExpirationDays] where TrainingID = currently entered TrainingID)

more ▼

asked Oct 08, 2012 at 08:00 PM in Default

avatar image

31 1 1 4

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first

It would be best to wrap this logic in a stored procedure, where you pass in the user entered fields, and work out the values for the expiration date.

Something along the lines of

 create proc InsertTraining 
 @EmployeeID int,
 @TrainingID int,
 @TrainingDate datetime
 set nocount on
 insert into TrainingTransactionTable 
    (EmployeeID, TrainingID, TrainingDate, ExpirationDate)
  dateadd(day, [ExpirationDays], @TrainingDate)
  TrainingID = @TrainingID
more ▼

answered Oct 08, 2012 at 08:40 PM

avatar image

Kev Riley ♦♦
66.8k 48 65 81

Thank you! I realized after I posted this that I don't really need to store the data, just display it on a report so I wound up dropping the field and creating a view. That being said, I do have another application for this answer.

Oct 08, 2012 at 11:44 PM gwhenning
(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.



asked: Oct 08, 2012 at 08:00 PM

Seen: 1435 times

Last Updated: Oct 09, 2012 at 07:31 AM

Copyright 2018 Redgate Software. Privacy Policy