Solved

T-SQL Fill in Field from Another Table

Posted on 2013-11-07
4
231 Views
Last Modified: 2013-11-07
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
0
Comment
Question by:RBS
  • 2
  • 2
4 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39630576
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
0
 

Author Comment

by:RBS
ID: 39630637
Great - thanks - just one question - I need the course name transferred with no date suffix where instance <1
0
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 39630658
you can do that using this:

update e

set e.Name =
case when instance > 0 then c.Name + ' ' + cast(year(e.date) as varchar(4)) + '-' + cast(month(e.date) as varchar(4))
else C.NAME end
from
Enrollments e
join Courses c on e.CourseId = c.Id
0
 

Author Closing Comment

by:RBS
ID: 39630665
Thanks ged325!
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

775 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