Solved

T-SQL Fill in Field from Another Table

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

Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

726 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