Link to home
Start Free TrialLog in
Avatar of RBS
RBS

asked on

T-SQL Fill in Field from Another Table

Hi:

I have two joined tabled - Courses and Enrollments structured as follows:

Courses
----------
Id
Name

Enrollments
---------------
Id
CourseId
Name
Instance
Date

I would like to go through the Enrollments table and create/change the name so that if the instance of the enrollment is greater than 0 (the student has taken the course more than once), the Name of the Enrollment would be the Course Name plus the Year and Month the student took the course.  For example, if the student took a second instance of Algebra this month, and the instance field was marked as 1, the Name of the Enrollment for that record would be 'Algebra 2013-11'

I'm having a problem wrapping my head around this.  Any help greatly appreciated.

RBS
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

update e
set e.Name = c.Name + ' ' + cast(year(e.date) as varchar(4)) + '-' + cast(month(e.date) as varchar(4))
from
Enrollments e
join Courses c on e.CourseId = c.Id
where instance > 0
Avatar of RBS
RBS

ASKER

Great - thanks - just one question - I need the course name transferred with no date suffix where instance <1
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RBS

ASKER

Thanks ged325!