x

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

gwhenning gravatar image

gwhenning
31 1 1 2

(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
)
as 
set nocount on

insert into TrainingTransactionTable 
   (EmployeeID, TrainingID, TrainingDate, ExpirationDate)
select
 @EmployeeID,
 @TrainingID,
 @TrainingDate,
 dateadd(day, [ExpirationDays], @TrainingDate)
from
 Training
where
 TrainingID = @TrainingID
more ▼

answered Oct 08, 2012 at 08:40 PM

Kev Riley gravatar image

Kev Riley ♦♦
54.3k 47 49 76

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

By RSS:

Answers

Answers and Comments

SQL Server Central

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

Topics:

x53
x9

asked: Oct 08, 2012 at 08:00 PM

Seen: 934 times

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