title: Create user defined function in SQL Server for dates not older than 14
body: <p>Not sure how to create this UDF. I have read through the existing questions but just not sure of the syntax.</p>
  
  <p>I want to save this query as a view but can't as I have declarations already</p>
  
  <pre><code>DECLARE @STARTDATE DATETIME
  DECLARE @ENDDATE DATETIME
  
  SELECT @ENDDATE = CAST(CONVERT(CHAR(10), GETDATE(), 101)+' 00:00:00' AS DATETIME)
  SELECT @STARTDATE = DATEADD(D, -1, @ENDDATE)--'-7'
  SELECT @ENDDATE = CAST(CONVERT(CHAR(10), GETDATE(), 101)+' 23:59:59' AS DATETIME)
  
  SELECT
      LTRIM(CIJ.ORDERACCOUNT) 'ACCOUNT'
      ,LTRIM(CIJ.SALESID) 'TOMKIN ORDER #'
      ,LTRIM(CIJ.INVOICEID) 'TOMKIN INVOICE #'
      ,CIJ.PURCHASEORDER 'YOUR ORDER #'
  FROM 
      CUSTINVOICEJOUR CIJ
  WHERE 
      CIJ.INVOICEDATE &gt;= @STARTDATE AND CIJ.INVOICEDATE &lt;= @ENDDATE
  ORDER BY 
      CIJ.INVOICEDATE,CIJ.INVOICEID
  </code></pre>
  
  <p>Any assistance would be very much appreciated</p>