Dear Programmers,
I need to compare one column value to another column in single select query.
As my query goes like this:
SELECT ATTNLOG.emp_id,
ATTNLOG.tdate,
ATTNLOG.tdate_out,
(CASE WHEN EXISTS (SELECT *
FROM tbl_emp_overtime
WHERE emp_id=ATTNLOG.emp_id
AND ot_indate=ATTNLOG.tdate)
THEN 0
ELSE 1
END) as 'STATUS'
FROM tbl_emp_attn_log ATTNLOG
WHERE ATTNLOG.emp_id=201
In my case condition i need to compare the every row of ATTNLOG and gives STATUS respectively.
Actually the result returns row values like
EMP_ID TDATE TDATE_OUT STATUS
201 2012-2-26 2012-2-26 0
201 2012-2-27 2012-2-27 1
and the table tbl_emp_overtime has value like
emp_ID ot_indate
201 2012-2-27
But my code gives like
EMP_ID TDATE TDATE_OUT STATUS
201 2012-2-26 2012-2-26 1
201 2012-2-27 2012-2-27 1
But it don’t give the result like that. Please help me out.
If you use sql 2008 cast the dates to
datedata type, if the original data type isdatetimeit should help: