Solved

# Return Zero instead of null

Posted on 2013-06-07
450 Views
I have a function below which works just fine but I would like to return 0 (Zero) instead of null.  I had a go here as you can see, but it does not work this way.

ALTER FUNCTION [dbo].[ufn_GetWorkedHoursWeekEnd]
(
@Month    varchar(50),
@Year int
)
RETURNS decimal(9,2)
BEGIN
DECLARE @WorkedHours decimal(9,2) =0
Select @WorkedHours = (SELECT Sum([TIME]) as Total FROM [Timesheets].[dbo].[TIMESHEET]
AND Year([Date]) = @Year
AND DATENAME(month, [Date]) = @Month
AND (datename(dw,[TIMESHEET].[Date]) = 'Saturday' OR
datename(dw,[TIMESHEET].[Date]) = 'Sunday'))

IF @WorkedHours = Null
BEGIN
SET @WorkedHours = 0
END

Return @WorkedHours

END
0
Question by:Kevin Robinson
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 7

Accepted Solution

Ross Turner earned 500 total points
ID: 39228370
Try:

isnull(@WorkedHours,0)

http://msdn.microsoft.com/en-us/library/ms184325.aspx

ALTER FUNCTION [dbo].[ufn_GetWorkedHoursWeekEnd]
(
@Month    varchar(50),
@Year int
)
RETURNS decimal(9,2)
BEGIN
DECLARE @WorkedHours decimal(9,2) =0
Select @WorkedHours = (SELECT Sum([TIME]) as Total FROM [Timesheets].[dbo].[TIMESHEET]
AND Year([Date]) = @Year
AND DATENAME(month, [Date]) = @Month
AND (datename(dw,[TIMESHEET].[Date]) = 'Saturday' OR
datename(dw,[TIMESHEET].[Date]) = 'Sunday'))

Return isnull(@WorkedHours,0)

END
0

LVL 38

Expert Comment

ID: 39228378
IF @WorkedHours = Null

-> IF @WorkedHours IS Null
0

LVL 25

Expert Comment

ID: 39228379
Return isnull(@WorkedHours, 0)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

### Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
###### Suggested Courses
Course of the Month9 days, 12 hours left to enroll