title: What is a datetime-type column in SQLite?
body: <p>I'm having to do some work where I am interacting with and creating tables that are manipulated by the <a href="https://github.com/praeclarum/sqlite-net" rel="nofollow noreferrer">SQLite.net</a> library. I need to be able to create tables that the other program won't know it didn't create itself. </p>
  
  <p>While looking at some schemas and getting a feel for how it creates things, I noticed something odd. When I read about <a href="https://www.sqlite.org/datatype3.html" rel="nofollow noreferrer">SQLite's data types</a>, I see that it's a small list, and initially it looked to me like if I want to store a date/time I need to make sure I use special functions in SELECT/INSERT/UPDATE queries, and the column can be TEXT or INTEGER or REAL. If I forget a datetime() function somewhere, I expect to get something back that isn't what I wanted (except for maybe a TEXT column.)</p>
  
  <p>So I'm looking at the schema for one of the tables, and I see this in the columns:</p>
  
  <pre><code>"LastUpdated" datetime ,
  </code></pre>
  
  <p>Uhh, OK. 'datetime' isn't a type. Or is it? The 'type affinity' rules indicate that 'datetime' is a valid type specifier and will be considered NUMERIC. OK, nothing really off there. I try a query:</p>
  
  <pre><code>sqlite&gt; select datetime(LastUpdated) from Foo;
  2016-01-06 15:37:36
  </code></pre>
  
  <p>That makes sense. I bet if I forget the datetime() function I get something goofy back, right?</p>
  
  <pre><code>sqlite&gt; select datetime(LastUpdated) from DestinationAdapter;
  2016-01-06 15:37:36
  </code></pre>
  
  <p>Uhhh... OK. That'd make sense if it's a TEXT column, I guess. So I made a test table:</p>
  
  <pre><code>sqlite&gt; CREATE TABLE TestDateTime ( 
     ...&gt;   "Id" integer primary key autoincrement not null,
     ...&gt;   "Value" TEXT,
     ...&gt;   "ValueN" NUMERIC);
  sqlite&gt; INSERT INTO TestDateTime(Value, ValueN) VALUES (datetime('2015-01-06 10:20:30'), datetime('2015-01-06 10:20:30'));
  sqlite&gt; select * from TestDateTime;
  1|2015-01-06 10:20:30|2015-01-06 10:20:30
  </code></pre>
  
  <p>OK. This is getting weird. It seems like SQLite is storing something that isn't the value discussed in the data type document. Whatever it is, it tells SQLite's console tool that the value in the column is a date/time value, and should be presented as such. So what exactly is going on? It seems like no matter what type I assign to a column, dates and times 'just work'. That's a little too magic to make me comfortable. What is really happening here, and how should it affect how I write my queries that involve date/time values?</p>