title: Explode Dates Between Dates, check and adjust parameter
body: <p>I have used a function from <a href="https://stackoverflow.com/questions/1378593/get-a-list-of-dates-between-two-dates-using-a-function">Get a list of dates between two dates using a function</a> which works brilliantly.</p>
  
  <p>However, I need to enter an additional check on the enddate that is passed. Sometimes this date will be null because if the record is still current, it will not have been entered. If it is, I would like to substitute it with the current date, so that the dates are returned from the start date to the current date.</p>
  
  <p>The code I want to enter is this, but wherever I try and put it, there are syntax errors.</p>
  
  <pre><code> IF @endddate IS NULL
       SET @enddate = getdate()
  </code></pre>
  
  <p>Here is the Code as it stands</p>
  
  <pre><code> CREATE FUNCTION [dbo].[ExplodeDates](@startdate datetime, @enddate datetime) 
   returns table as 
   return ( 
   with  
   N0 as (SELECT 1 as n UNION ALL SELECT 1) 
  ,N1 as (SELECT 1 as n FROM N0 t1, N0 t2) 
  ,N2 as (SELECT 1 as n FROM N1 t1, N1 t2) 
  ,N3 as (SELECT 1 as n FROM N2 t1, N2 t2) 
  ,N4 as (SELECT 1 as n FROM N3 t1, N3 t2) 
  ,N5 as (SELECT 1 as n FROM N4 t1, N4 t2) 
  ,N6 as (SELECT 1 as n FROM N5 t1, N5 t2) 
  ,nums as (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 1)) as num FROM N6) 
  SELECT DATEADD(day,num-1,@startdate) as thedate 
  FROM nums 
  WHERE num &lt;= DATEDIFF(day,@startdate,@enddate) + 1 
   );
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>