Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert select statement

Status
Not open for further replies.

saintedmunds

Technical User
Apr 7, 2006
78
GB
HI

I am Coverting from an access mdb to sql 2005 and I have the below select staement which i seem unable to convert.

SELECT tTimeTable.BoxID, [Subject] & Chr(13) & Chr(10) & Concatenate("SELECT tStaff.StaffInitials FROM tTimeTable INNER JOIN tStaff ON tTimeTable.StaffID = tStaff.StaffID WHERE tTimeTable.BoxID=" & tTimeTable.BoxID & " AND tTimeTable.DayID=1") AS SubjectStaff, tTimeTable.DayID
FROM tTimeTable LEFT JOIN tSubject ON tTimeTable.SubjectID = tSubject.SubjectID
WHERE (((tTimeTable.DayID)=1));


Any help would be great

Cheers
 
Try:
Code:
SELECT tTimeTable.BoxID,
       [Subject] + Char(13) + Char(10) +
       'SELECT tStaff.StaffInitials FROM tTimeTable
               INNER JOIN tStaff ON tTimeTable.StaffID = tStaff.StaffID
               WHERE tTimeTable.BoxID=' +
                     CAST(tTimeTable.BoxID as varchar(20)) + ' AND  tTimeTable.DayID=1' AS SubjectStaff,
       tTimeTable.DayID
FROM tTimeTable
      LEFT JOIN tSubject ON tTimeTable.SubjectID = tSubject.SubjectID
WHERE tTimeTable.DayID=1

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Hi

Thank you for getting back to me so quick.
But the result comes out like this:

Maths
SELECT tStaff.StaffInitials FROM tTimeTable
INNER JOIN tStaff ON tTimeTable.StaffID = tStaff.StaffID
WHERE tTimeTable.BoxID=1 AND tTimeTable.DayID=1

Any Ideas?

Cheers
 
How do you want the result to looks like?
If you want the SELECT to be on one row just change it to be on one row:
Code:
[Subject] + Char(13) + Char(10) +
'SELECT tStaff.StaffInitials FROM tTimeTable INNER JOIN tStaff ON tTimeTable.StaffID = tStaff.StaffID WHERE tTimeTable.BoxID=' + CAST(tTimeTable.BoxID as varchar(20)) + ' AND  tTimeTable.DayID=1' AS SubjectStaff

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Hi

Sorry maybe I didnt make my self clear.

The reults im getting is actually display the select statement when i should get something like this

BoxID SubjectStaff DayID
6 English 1
HBP, IvO

Where as im getting

BoxID SubjectStaff DayID
6 SELECT tStaff.StaffInitials FROM tTimeTable
INNER JOIN tStaff ON tTimeTable.StaffID = tStaff.StaffID
WHERE tTimeTable.BoxID=1 AND tTimeTable.DayID=1

Do you see what i mean?

 
OOOOOOOOO :), now I understand :)
Try:
Code:
SELECT tTimeTable.BoxID,
       [Subject] + Char(13)+Char(10)+
       Tbl1.StaffInitials AS SubjectStaff,
       tTimeTable.DayID
FROM tTimeTable
     LEFT JOIN tSubject ON tTimeTable.SubjectID = tSubject.SubjectID
     LEFT JOIN (SELECT tStaff.StaffInitials, tTimeTable.BoxID
                       FROM tTimeTable 
                       INNER JOIN tStaff ON tTimeTable.StaffID = tStaff.StaffID
                       WHERE tTimeTable.DayID=1) Tbl1
     ON tTimeTable.BoxId = Tbl1.BoxID
WHERE tTimeTable.DayID=1

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Hi

We are almost there but it is only picking up one staff initial somtimes there is more than one that is why i had to use the Concatenate function.

The initals are seperated by a ,

Any ideas?

Cheers again
 
No, In TSQL you didn't have Concatenate function. If you want to Concatenate read FAQ183-6466 to see how to do it.

Borislav Borissov
VFP9 SP1, SQL Server 2000/2005.
MVP VFP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top