Solved

Return Zero instead of null

Posted on 2013-06-07
3
450 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 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 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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

740 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