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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Select return results from two tables

Author  Topic 

dprichard
Yak Posting Veteran

94 Posts

Posted - 2006-08-22 : 16:31:43
I have a query that has a subquery in it. I need to return a total even if the subquery returns no results. I am not sure how to return the results even if the subquery value is null. If it is null I want the sub query to equal zero.

Here is my Select Statement



SELECT employee.emp_id, DATEDIFF(mm, employee.emp_begin_accrual, GETDATE()) * employee.emp_accrual_rate -
(SELECT SUM(request_duration) AS daystaken
FROM request) AS daysleft, employee.emp_lname + ', ' + employee.emp_fname + ' ' + employee.emp_minitial + '.' AS emp_name,
department.department_name, location.location_name
FROM employee INNER JOIN
request AS request_1 ON employee.emp_id = request_1.emp_id INNER JOIN
department ON employee.emp_department = department.department_id INNER JOIN
location ON department.department_location = location.location_id
GROUP BY employee.emp_id, employee.emp_begin_accrual, employee.emp_accrual_rate, employee.emp_fname, employee.emp_minitial,
employee.emp_lname, department.department_name, location.location_name
ORDER BY location.location_name, department.department_name, employee.emp_lname



I need the section below to equal zero if it returns a null result:



(SELECT SUM(request_duration) AS daystaken
FROM request)

timmy
Master Smack Fu Yak Hacker

1242 Posts

Posted - 2006-08-22 : 17:45:13
Just use IsNull:
IsNull((SELECT SUM(request_duration) AS daystaken FROM request),0)

or
(SELECT IsNull(SUM(request_duration), 0) AS daystaken FROM request)
Go to Top of Page
   

- Advertisement -