Solved

T-SQL Fill in Field from Another Table

Posted on 2013-11-07
4
235 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

828 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