Solved

T-SQL Fill in Field from Another Table

Posted on 2013-11-07
4
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

732 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