Joe Law
Joe Law

Reputation: 43

SQL calculated query for start time and duration to find end time

This is my current SQL Select query;

SELECT
    Name AS 'Class Name', FirstName + ' ' + LastName AS 'Instructor',
    LessonDuration AS 'Lesson Duration (hrs)', LessonTime AS 'Lesson Start' 
FROM 
    Lesson, Class, Instructor 
WHERE 
    Lesson.ClassID = Class.ClassID 
    AND Lesson.InstructorID = Instructor.InstructorID 
    AND LessonDay = 'Monday' 
ORDER BY 
    LessonTime ASC;

I have a table Lesson with some columns, but most importantly LessonDuration (in hours) and LessonTime (which is the start time). In a select query, I would like to have an additional column that displays the end time. This would involve calculating the additional hours put onto the start time using the duration but I am unsure how to do so.

I am using SQL Server built into Visual Studio 2013.

Upvotes: 0

Views: 862

Answers (1)

Serkan Arslan
Serkan Arslan

Reputation: 13393

You can use this.

SELECT Name AS 'Class Name', FirstName + ' ' + LastName AS 'Instructor',
LessonDuration AS 'Lesson Duration (hrs)', LessonTime AS 'Lesson Start' , DATEADD(HOUR,LessonDuration,LessonTime) 'End Time'
FROM Lesson, Class, Instructor 
WHERE Lesson.ClassID = Class.ClassID 
AND Lesson.InstructorID = Instructor.InstructorID 
AND LessonDay = 'Monday' 
ORDER BY LessonTime ASC;

Upvotes: 1

Related Questions