Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select value from table

Author  Topic 

jafrywilson
Constraint Violating Yak Guru

379 Posts

Posted - 2011-03-22 : 03:21:56
Hi all,

I need to select a field by passing the id

My table (courses) is like this

Courseid coursename

1 School
2 Engg
3 Arts

And my second table is like this

Table (Exam)

Examname cousreid

Annual 3,2
Half_yearly 3,1,2

Passing the courseid in table exam i need to get the coursename from courses table

I need Output like this

examname coursename
Annual Arts
Annual Engg
Half_yearly Arts
Half_yearly Engg
Half_yearly School

ahmeds08
Aged Yak Warrior

737 Posts

Posted - 2011-03-22 : 03:33:44
SELECT E.EXAMNAME,C.COURSENAME FROM EXAMS E
INNER JOIN COURSES C
ON C.COURSEID=E.COURSEID
WHERE E.COURSEID=1
Go to Top of Page

jafrywilson
Constraint Violating Yak Guru

379 Posts

Posted - 2011-03-22 : 03:46:01
Thnx for your reply..

I tried this


SELECT E.EXAMNAME,C.COURSENAME FROM EXAMS E
INNER JOIN COURSES C
ON CONVERT(VARCHAR,C.CourseID)=E.CourseID
WHERE E.CourseID=1


But i am getting this error

Msg 245, Level 16, State 1, Line 2
Conversion failed when converting the varchar value '3,2' to data type int.

Go to Top of Page

jafrywilson
Constraint Violating Yak Guru

379 Posts

Posted - 2011-03-22 : 03:55:02
And the exam table has the courseid like this 2,3,1
So it throws this error
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-03-22 : 04:00:43
SELECT E.EXAMNAME,C.COURSENAME FROM EXAMS E
INNER JOIN COURSES C
ON ','+E.COURSEID+',' like '%,'+cast(C.COURSEID as varchar(10))+',%'
WHERE ','+E.COURSEID+',' like ',1,'

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -