Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's my simple query. If I query a record that doesn't exist then I will get nothing returned. I'd prefer that 0 is returned in that scenario. Please help me out from this.Thanks in advance

SQL
SELECT *
    FROM (
        SELECT DISTINCT emp.EMP_CODE
            ,emp.EMP_NAME
            ,ROW_NUMBER() OVER (
                PARTITION BY emp.emp_code ORDER BY emp.EFFECTIVE_FROM DESC
                ) AS Row
            ,CASE 
                WHEN et.DAY = @d1
                    AND et.MONTH = @m1
                    AND et.YEAR = @y1
                    AND et.EMP_CODE = emp.EMP_CODE
                    AND et.site_id = @SITE_ID
                    THEN et.GENERAL_SHIFT
                ELSE '0'
                END AS G
            ,CASE 
                WHEN et.DAY = @d1
                    AND et.MONTH = @m1
                    AND et.YEAR = @y1
                    AND et.EMP_CODE = emp.EMP_CODE
                    AND et.site_id = @SITE_ID
                    THEN et.FIRST_SHIFT
                ELSE '0'
                END AS F
            ,CASE 
                WHEN et.DAY = @d1
                    AND et.MONTH = @m1
                    AND et.YEAR = @y1
                    AND et.EMP_CODE = emp.EMP_CODE
                    AND et.site_id = @SITE_ID
                    THEN et.SECOND_SHIFT
                ELSE '0'
                END AS S
            ,CASE 
                WHEN et.DAY = @d1
                    AND et.MONTH = @m1
                    AND et.YEAR = @y1
                    AND et.EMP_CODE = emp.EMP_CODE
                    AND et.site_id = @SITE_ID
                    THEN et.THIRD_SHIFT
                ELSE '0'
                END AS T
        FROM dbo.EMP_FPONM_EMPLOYEE_INFORMATION emp
        LEFT JOIN dbo.EMP_FPONM_EMP_TIME_SHEET et ON emp.EMP_CODE = et.EMP_CODE
        WHERE EMP.SITE_ID = @SITE_ID
            AND emp.DEPT_NAME = @DEPT_NAME
            AND emp.STATUS = 'Present'
            AND et.DAY = @d1
            AND et.MONTH = @m1
            AND et.YEAR = @y1
        ) a
    WHERE a.Row = 1


What I have tried:

If I query a record that doesn't exist then I will get nothing returned. I'd prefer that 0 is returned in that scenario.
Posted
Updated 4-Mar-16 2:46am

1 solution

That's something I would normally handle in my presentation layer but if you must do it this way then have a look at using EXISTS[^]
For example: (although you should probably invert this IF statement)
SQL
IF NOT EXISTS 
(
	SELECT emp.EMP_CODE 
        FROM dbo.EMP_FPONM_EMPLOYEE_INFORMATION emp
        LEFT JOIN dbo.EMP_FPONM_EMP_TIME_SHEET et ON emp.EMP_CODE = et.EMP_CODE
        WHERE EMP.SITE_ID = @SITE_ID
            AND emp.DEPT_NAME = @DEPT_NAME
            AND emp.STATUS = 'Present'
            AND et.DAY = @d1
            AND et.MONTH = @m1
            AND et.YEAR = @y1
)
	SELECT 0 as EMP_CODE, '' AS EMP_NAME, 0 AS Row, '0' AS G, '0' AS F, '0' AS S, '0' AS T
ELSE
	-- your original query in here
END

As an aside, I sincerely hope that you are not storing numeric values in varchar fields :-)
 
Share this answer
 
Comments
Member 12369428 18-Mar-16 5:47am    
hi using your above example i completed my query but i got some other with same query
recently i registered new employee that employee data is not comining some days
CHill60 18-Mar-16 5:52am    
What do you mean "some days"? Do you have an example?
Member 12369428 18-Mar-16 5:48am    
Please suggest me anything thanks in advance.
Member 12369428 18-Mar-16 5:56am    
ok i will post my query
Member 12369428 18-Mar-16 6:00am    
IF NOT EXISTS
(
SELECT emp.EMP_CODE,emp.EMP_NAME
from dbo.EMP_FPONM_EMPLOYEE_INFORMATION emp
join dbo.EMP_FPONM_EMP_TIME_SHEET et on emp.EMP_CODE=et.EMP_CODE
where EMP.SITE_ID=@SITE_ID AND emp.DEPT_NAME=@DEPT_NAME AND emp.STATUS='Present'
and et.DAY=01 and et.MONTH=03 and et.YEAR=16
)
INSERT INTO #temp5(EMP_CODE,EMP_NAME,Row,G4,F4,S4,T4)
select * from(
select emp.EMP_CODE,emp.EMP_NAME,ROW_NUMBER() OVER(PARTITION BY emp.emp_code ORDER BY emp.EFFECTIVE_FROM DESC) AS Row,
'0' AS G4, '0' AS F4, '0' AS S4, '0' AS T4
from dbo.EMP_FPONM_EMPLOYEE_INFORMATION emp
join dbo.EMP_FPONM_EMP_TIME_SHEET et on emp.EMP_CODE=et.EMP_CODE
where EMP.SITE_ID=@SITE_ID AND emp.DEPT_NAME=@DEPT_NAME AND emp.STATUS='Present'

)a where a.Row=1
ELSE
INSERT INTO #temp5 (EMP_CODE,EMP_NAME,Row,G4,F4,S4,T4)
select * from(
select distinct emp.EMP_CODE,emp.EMP_NAME,ROW_NUMBER() OVER(PARTITION BY emp.emp_code ORDER BY emp.EFFECTIVE_FROM DESC) AS Row,
case when et.DAY=@d5 and et.MONTH=@m5 and et.YEAR=@y5 and et.EMP_CODE=emp.EMP_CODE and et.site_id=@SITE_ID then et.GENERAL_SHIFT else 0 end as G4,
case when et.DAY=@d5 and et.MONTH=@m5 and et.YEAR=@y5 and et.EMP_CODE=emp.EMP_CODE and et.site_id=@SITE_ID then et.FIRST_SHIFT else 0 end as F4,
case when et.DAY=@d5 and et.MONTH=@m5 and et.YEAR=@y5 and et.EMP_CODE=emp.EMP_CODE and et.site_id=@SITE_ID then et.SECOND_SHIFT else 0 end as S4,
case when et.DAY=@d5 and et.MONTH=@m5 and et.YEAR=@y5 and et.EMP_CODE=emp.EMP_CODE and et.site_id=@SITE_ID then et.THIRD_SHIFT else 0 end as T4
from dbo.EMP_FPONM_EMPLOYEE_INFORMATION emp
left join dbo.EMP_FPONM_EMP_TIME_SHEET et on emp.EMP_CODE=et.EMP_CODE
where EMP.SITE_ID=@SITE_ID AND emp.DEPT_NAME=@DEPT_NAME AND emp.STATUS='Present'
and et.DAY=01 and et.MONTH=03 and et.YEAR=16
)a where a.Row=1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900