October 5, 2010 at 7:12 am
Hi,
I am getting headcount as null for if there is no value for that particular date.Instead of that i should return 0.Please see my stored procedure.
alter PROCEDURE SAR_Sp_GetForecastedHC(@date1 DATETIME, @date2 DATETIME, @date3 DATETIME, @date4 DATETIME,@CampaignID int)
AS
DECLARE @query VARCHAR(MAX)
BEGIN
SET @query = 'SELECT '+ CHAR(39) + 'Forecasted HC as per Hiring Plan' + CHAR(39) + ' AS HeadCount, [' + CONVERT(VARCHAR,@date1,101) + '], [' + CONVERT(VARCHAR,@date2,101)+ '], [' + CONVERT(VARCHAR,@date3,101) + '], [' + CONVERT(VARCHAR,@date4,101) + ']' +
'FROM
(SELECT HeadCount,
StartDate FROM SAR_HeadCount where CampaignID=@CampaignID)
AS SourceTable
PIVOT
(
max(HeadCount)
FOR StartDate IN (' + '[' + CONVERT(VARCHAR,@date1,101) + '], [' + CONVERT(VARCHAR,@date2,101)+ '], [' + CONVERT(VARCHAR,@date3,101) + '], [' + CONVERT(VARCHAR,@date4,101) + ']' + ')
) AS PivotTable'
EXEC(@query)
END
October 5, 2010 at 7:43 am
StartDate FROM SAR_HeadCount where CampaignID=' + CAST(@CampaignID AS VARCHAR) + ')
@CampaignID is out of scope otherwise.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 5, 2010 at 7:49 am
Hi,
In the above stored procedure if i dont have HeadCount for particular date i am getting value as null but i should get value as 0.Can u tell me how to do that.
October 5, 2010 at 7:53 am
naresh0407 93367 (10/5/2010)
Hi,In the above stored procedure if i dont have HeadCount for particular date i am getting value as null but i should get value as 0.Can u tell me how to do that.
ISNULL(value as null, 0)
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 5, 2010 at 9:54 am
Hi Chris,
Can u tell where i can u in the above stored procedur.
October 5, 2010 at 10:01 am
naresh0407 93367 (10/5/2010)
Hi Chris,Can u tell where i can u in the above stored procedur.
Naresh, I'm sorry - can I have that in English please?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 5, 2010 at 10:04 am
Hi,
Please tell me how to use this function ISNULL(value as null, 0) in the stored procedure.
October 5, 2010 at 10:17 am
Why don't you put your dates into your table source instead? Like this:
'FROM
(SELECT HeadCount,
StartDate FROM SAR_HeadCount where CampaignID = ' + @CampaignID +
' AND StartDate IN (' + @date1 + ', ' + @date2 + ', ' + @date3 + ', ' + @date4 + '))
AS SourceTable
Then you won't have null values for headcount.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply