Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi if I am giving @startdate = 01-01-2015 and @enddate = 01-31-2015 as parameters. If data is not there for 01-20-2015 then need to return value as 0 else return data from table. Can any help please..!
Posted
Comments
OriginalGriff 3-Mar-15 7:57am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Perhaps an example of your query, and sample table data to show what you want it to return in each case?
Use the "Improve question" widget to edit your question and provide better information.
ZurdoDev 3-Mar-15 8:23am    
One way is to union with a select statement that returns 0 for every day and then you add them together.

Generate a list of dates within the range required and join that with your table using the ISNULL function to derive your default value.

For example I created some simple sample data like this
SQL
CREATE TABLE CP5
(
    aDate date,
    aValue int
)

insert into CP5 values(getdate() - 1,1)
insert into CP5 values(getdate() - 2,2)
insert into CP5 values(getdate() - 3,3)
insert into CP5 values(getdate() - 5,5)
insert into CP5 values(getdate() - 6,7)
Set up my start/end dates as follows
SQL
DECLARE @startdate date
SET @startdate = getdate() - 7  -- i.e. 2015-02-24
DECLARE @enddate date
SET @enddate = getdate();       -- i.e. 2015-03-03

I can now use this query to get all of the dates in the range with either a value from the table or 0
SQL
WITH q AS
        (
        SELECT  @startdate AS datum
        UNION ALL
        SELECT  DATEADD(dd, 1, datum)
        FROM q WHERE  DATEADD(dd, 1, datum) < @enddate
        )
SELECT  datum, ISNULL(aValue, 0) as aValue
    FROM    q
    LEFT OUTER JOIN CP5 ON q.datum = CP5.aDate

Gives me these results
2015-02-24  0
2015-02-25  7
2015-02-26  5
2015-02-27  0
2015-02-28  3
2015-03-01  2
2015-03-02  1


The idea for the CTE was adapted from the Tip Generating a Sequence in SQL[^] by Manas Bhardwaj (My name also appears next to that tip but I can't take credit for the date sequence bit at all)
 
Share this answer
 
This query can help you.

select isnull(Column1,0) , isnull(Column2,0) from TableName where Yourdate between @StartDate and @EndDate


Good luck.
 
Share this answer
 
Comments
CHill60 3-Mar-15 8:59am    
This does not do what the OP wanted - return a zero if the default value is not present. This will only return 0 if the date exists but the column with the value is null
[no name] 3-Mar-15 23:21pm    
What do you mean "If data is not there for 01-20-2015". if there is no data then what will be the value of that row blank or null for 01-20-2015.
Hi Susheel.. nulls are not present in table.
 
Share this answer
 
Comments
CHill60 3-Mar-15 8:59am    
Use the Have a Question or comment link next to a post - do not respond by posting a "solution"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900