?
Solved

Return Zero instead of null

Posted on 2013-06-07
3
Medium Priority
?
457 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
[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
  • Learn & ask questions
3 Comments
 
LVL 7

Accepted Solution

by:
Ross Turner earned 2000 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 38

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Suggested Courses

752 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