title: Python - Passing datetime parameters into a SQL Command
body: <p>I am trying to do something like this in Python, </p>
  
  <pre><code>SQLCommand = ("Delete From %s where [Date] &gt;= %s and [Date] &lt;= %s", (calendar_table_name, required_starting_date, required_ending_date))
  
  cursor.execute(SQLCommand)
  </code></pre>
  
  <p><code>calendar_table_name</code> is a <code>string</code> variable</p>
  
  <p><code>required_starting_date</code> is a <code>datetime</code> variable</p>
  
  <p><code>required_ending_date</code> is a <code>datetime</code> variable</p>
  
  <p>Trying this gives me an error:</p>
  
  <blockquote>
    <p>The first argument to execute must be a string or unicode query.</p>
  </blockquote>
  
  <p>Tried this and it gives me the same error:</p>
  
  <pre><code>SQLCommand = ("Delete From " +  calendar_table_name + " where [Date] &gt;= %s and [Date] &lt;= %s", ( required_starting_date, required_ending_date))
  
  cursor.execute(SQLCommand)
  </code></pre>
  
  <p>Edit:</p>
  
  <pre><code>type(required_ending_date)
  
  Out[103]: pandas._libs.tslibs.timestamps.Timestamp
  
  
  type(required_starting_date)
  
  Out[103]: pandas._libs.tslibs.timestamps.Timestamp
  </code></pre>
  
  <p>This works in SSMS for me,</p>
  
  <pre><code>  delete from [table_test] where [Date] &gt;= '2007-01-01' and [Date] &lt;= '2021-01-01';
  </code></pre>
  
  <p><strong>Update :- This is the code, that I am trying with</strong> </p>
  
  <pre><code>Delete_SQLCommand =  f"Delete FROM [{calendar_table_name}] WHERE [Date]&gt;=? And [Date]&lt;=?"
  params = (required_starting_date, required_ending_date)
  </code></pre>
  
  <p>required_starting_date &amp; required_ending_date are of "TimeStamp" formats</p>
  
  <pre><code>calendar_tbl_connection = pyodbc.connect(driver=driver, server=required_server, database=database_name,
                       trusted_connection='yes')   
  calendar_tbl_cursor = calendar_tbl_connection.cursor()
  calendar_tbl_cursor.execute(Delete_SQLCommand,params)
  calendar_tbl_connection.commit
  calendar_tbl_connection.close()
  </code></pre>