473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to handle null values in stored procedure with pivot

45
Hi,

I am using this stored procedure with pivot.If i dont have data i am getting null with this stored procedure.Can u tell me how to handle null.below query is pivot.

I am using like this but getting erro
max(isnull(Head,0))
Expand|Select|Wrap|Line Numbers
  1. PIVOT
  2. (
  3. max(Head)
  4. FOR StartDate IN (' + '[' + CONVERT(VARCHAR,@date1,101) + '], [' + CONVERT(VARCHAR,@date2,101)+ '], [' + CONVERT(VARCHAR,@date3,101) + '], [' + CONVERT(VARCHAR,@date4,101) + ']' + ')
  5. ) AS PivotTable' 
  6.  
  7.  
  8. ALTER PROCEDURE [dbo].[SAR_Sp_GetForecastedHC](@date1 DATETIME, @date2 DATETIME, @date3 DATETIME, @date4 DATETIME,@CampaignId int)
  9. AS
  10. DECLARE @query VARCHAR(MAX)
  11. BEGIN
  12. 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) + ']' +
  13. 'FROM
  14. (SELECT HeadCount as Head,
  15. StartDate FROM SAR_HeadCount where CampaignID=' + CAST(@CampaignID AS VARCHAR) + ')
  16. AS SourceTable
  17. PIVOT
  18. (
  19. max(Head)
  20. FOR StartDate IN (' + '[' + CONVERT(VARCHAR,@date1,101) + '], [' + CONVERT(VARCHAR,@date2,101)+ '], [' + CONVERT(VARCHAR,@date3,101) + '], [' + CONVERT(VARCHAR,@date4,101) + ']' + ')
  21. ) AS PivotTable' 
  22. EXEC(@query)
  23. END
Oct 30 '10 #1
1 2240
Please try this query..

Enjoy Knowledge FREEDOM

Expand|Select|Wrap|Line Numbers
  1. ALTER PROCEDURE [dbo].[SAR_Sp_GetForecastedHC](@date1 DATETIME, @date2 DATETIME, @date3 DATETIME, @date4 DATETIME,@CampaignId int) 
  2. AS 
  3. DECLARE @query VARCHAR(MAX) 
  4. BEGIN 
  5. SET @query = 'SELECT '+ CHAR(39) + 'Forecasted HC as per Hiring Plan' + CHAR(39) + ' AS HeadCount, isNull([' + CONVERT(VARCHAR,@date1,101) + '],0), isNull([' + CONVERT(VARCHAR,@date2,101)+ '],0), isNull([' + CONVERT(VARCHAR,@date3,101) + '],0), isNull([' + CONVERT(VARCHAR,@date4,101) + '],0)' + 
  6. 'FROM 
  7. (SELECT HeadCount as Head, 
  8. StartDate FROM SAR_HeadCount where CampaignID=' + CAST(@CampaignID AS VARCHAR) + ') 
  9. AS SourceTable 
  10. PIVOT 
  11. max(Head) 
  12. FOR StartDate IN (' + '[' + CONVERT(VARCHAR,@date1,101) + '], [' + CONVERT(VARCHAR,@date2,101)+ '], [' + CONVERT(VARCHAR,@date3,101) + '], [' + CONVERT(VARCHAR,@date4,101) + ']' + ') 
  13. ) AS PivotTable'  
  14. EXEC(@query) 
  15. END
Nov 5 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Gaz | last post by:
I have a table which has 10 columns which make up the secondary key. 1 or more of these columns can be set but the remaining columns in the secondary key will be null. For example : id k1 k2...
3
by: John Smith | last post by:
Hello all: Does anyone happen to know the best way to handle null values for DateTimePickers? We have quite a few columns in our db that will not necissarily have a date for certain entries. ...
1
by: DKode | last post by:
I find myself writing repetitive functions for handling null values from my DB like so: Private Function SetDateNull(ByVal p_date As Object) As Date If (TypeOf (p_date) Is System.DBNull) Then...
11
by: eddy de boer | last post by:
Hello, in my aspx page I have the followong code: <asp:Repeater id="Repeater1" runat="server"> <ItemTemplate> .... <%# Server.HtmlDecode((string)DataBinder.Eval(Container.DataItem,"tekst"))...
0
by: Jason Coyne | last post by:
Here is another programming blog entry. Sorry for the normal friends :) I recently ran into a problem with a web service I was trying to call from c# where the web service returned null dates...
2
by: rn5a | last post by:
How do I pass a NULL value to a field while inserting records in a SQL Server 2005 DB table using a stored procedure? I tried the following but it inserts an empty string & not a NULL value: ...
3
by: psycho | last post by:
how should i hanle null values returned by the datareader i am using OleDbDataReader for accessing access database.
2
by: jehugaleahsa | last post by:
Hello: Is there a way to handle null values for ints, DateTime, etc. in generated DataTables? It seems it is not something the Microsoft crew thought of. That is hard for me to believe. ...
5
by: tonialbrown | last post by:
I have some code that updates a record's fields based on a selection from a list box (lstDelFrom). The user selects the record from the list box & it copies it into the fields , , etc. My...
1
by: apache626 | last post by:
I really need help with this one. I am using a script task to write data pulled from a MS SQL DB to a fixed width file. The script takes the ItemArray and writes the value of the column to the file...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.