Solved

Return Zero instead of null

Posted on 2013-06-07
3
444 Views
Last Modified: 2013-06-07
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]
(
@UserName    varchar(50),
@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]
                                    WHERE [IdVolNow] = @Username
                                    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
Comment
Question by:Kevin Robinson
3 Comments
 
LVL 7

Accepted Solution

by:
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]
(
@UserName    varchar(50),
@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]
                                    WHERE [IdVolNow] = @Username
                                    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 37

Expert Comment

by:Gerwin Jansen, EE MVE
ID: 39228378
IF @WorkedHours = Null

-> IF @WorkedHours IS Null
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39228379
Return isnull(@WorkedHours, 0)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question