Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server, Select statement inside a 'case'

Can anyone help me with the right syntax in SQL Server, I have something that looks like this inside a larger query

VSCS.ISCOTEACH,
VSCS.NoMARKS,
CASE (SELECT COUNT(*) FROM tbl_CoTeacher COTH WHERE    COTH.CLAS_METG_TIME_PK=VSCS.CLAS_METG_TIME_PK
        AND ISNULL(COTH.DELT_FLAG,0)=0) > 0 THEN 1 ELSE 0 END AS HASCOTEACH),

The third line on is not correct of course, pretty much I want a column called HASCOTEACH to contain a 1 if the query SELECT COUNT(*) FROM tbl_CoTeacher COTH WHERE COTH.CLAS_METG_TIME_PK=VSCS.CLAS_METG_TIME_PK AND ISNULL(COTH.DELT_FLAG,0)=0 returns a value greater than 1, 0 otherwise.

like image 276
Art F Avatar asked Jul 02 '13 15:07

Art F


1 Answers

You can try like this..

(CASE when (SELECT COUNT(*) FROM tbl_CoTeacher COTH WHERE    COTH.CLAS_METG_TIME_PK=VSCS.CLAS_METG_TIME_PK
        AND ISNULL(COTH.DELT_FLAG,0)=0) > 0 THEN 1 ELSE 0 END) AS HASCOTEACH
like image 171
Amit Singh Avatar answered Nov 10 '22 01:11

Amit Singh