title: Checking for invalid date in SQL Server Stored Procedure
body: <p>I have a stored procedure in SQL Server 2008 that will insert a record into a table.</p>
  
  <p>The sproc looks similar to this:</p>
  
  <pre><code>    -- params
  @f1 int, 
  @f2 datetime = null, 
  @f3 datetime,
  @f4 bit = 0 
  
  BEGIN
      INSERT INTO dbo.table
      (fields......)
      VALUES
      (@params.....)
  
      RETURN @@IDENTITY
  END
  </code></pre>
  
  <p>Prior to running the INSERT statement, I would like to trap the datetime value of the f2 param. If f2 is not a valid date I would like to set it to null.</p>
  
  <p>I'm fuzzy on the syntax on how to do this. I thought the following would suffice, but not so:</p>
  
  <pre><code>--params
  @f1..
  @f2..
  ...
  
  IF(ISDATE(@f2) &lt; 1)
  SET @f2= NULL
  
  BEGIN
   INSERT...
   ...
  END
  </code></pre>
  
  <p>What is the syntax for trapping and resetting a param value prior to executing an INSERT?</p>
  
  <p>By the way, the reason that led me to finding a solution to this problem is rooted in the fact that I'm passing a C# minimum date value of "1/1/0001" to SQLS which supports minimum date value of "1/1/1753".</p>