title: Writing dates to SQL Server data from R
body: <p>I am saving a <code>data.frame</code> through odbc to an SQL Server database in a new table. The <code>data.frame</code> contains dates in one column.</p>
  
  <p>I am using the following commands:</p>
  
  <pre><code>require(odbc)
  require(DBI)
  
  MyData &lt;- data.frame(Date = as.Date(c("2017-05-17","2017-05-18"), format = "%Y-%m-%d"))
  
  conn &lt;- DBI::dbConnect(drv = odbc::odbc(), dsn='MyDatabaseSource')
  
  dbWriteTable(conn = conn, name = 'dbo.MyTable', value = MyData, overwrite = T)
  </code></pre>
  
  <p>However, this results in the following error message:</p>
  
  <pre><code>Error in result_insert_dataframe(rs@ptr, values) : nanodbc/nanodbc.cpp:1587: 22007: [Microsoft][ODBC SQL Server Driver][SQL Server]Conversion failed when converting date and/or time from character string.
  </code></pre>
  
  <p>I can resolve it by changing the column type to <code>character</code>, but then the column is stored as <code>varchar(255)</code> in the database. How do I store the Date column in the date format in the database and not <code>varchar</code>?</p>