64

Is there a way to create a table in sqlite3 that has a datetime column that defaults to 'now'?

The following statement returns a syntax error:

create table tbl1(id int primary key, dt datetime default datetime('now'));

Update: Here's the correct ddl courtesy of Sky Sanders:

create table tbl1(id int primary key, dt datetime default current_timestamp);
3
  • @NobodyMan - perhaps you could update your question and include the ddl that works for others to easily find. Apr 11, 2010 at 0:31
  • 1
    @Sky - I'm not opposed, but doesn't that make your answer redundant? Also, if I do edit should I replace the incorrect snippet or simply append to the question with the correct answer? I'm a bit ignorant of SO etiquette :-)
    – NobodyMan
    Apr 14, 2010 at 17:10
  • 1
    It doesn't really diminish an answer when you ADD an update to your question with an example of what ultimately worked for you. A lot of answers can be helpful suggestions, as mine was with the ddl fragment and background, which helped you fix your ddl. Confirming the solution by adding the correct statement is a value add. Under no circumstances change the original code, that is how someone with the same problem will find this solution. Anyway, is a moot point, I edited my answer to present a working version of your ddl. cheers Apr 14, 2010 at 18:51

4 Answers 4

104

Try this:

create table tbl1(id int primary key, dt datetime default current_timestamp);

Background:

The DEFAULT constraint specifies a default value to use when doing an INSERT. The value may be NULL, a string constant, a number, or a constant expression enclosed in parentheses. The default value may also be one of the special case-independant keywords CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP. If the value is NULL, a string constant or number, it is inserted into the column whenever an INSERT statement that does not specify a value for the column is executed. If the value is CURRENT_TIME, CURRENT_DATE or CURRENT_TIMESTAMP, then the current UTC date and/or time is inserted into the columns. For CURRENT_TIME, the format is HH:MM:SS. For CURRENT_DATE, YYYY-MM-DD. The format for CURRENT_TIMESTAMP is "YYYY-MM-DD HH:MM:SS".

From http://www.sqlite.org/lang_createtable.html

1
  • 11
    Note that current_timestamp is precise to the second, if you want milliseconds use default (STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')).
    – TWiStErRob
    Mar 7, 2015 at 23:05
18
... default (datetime(current_timestamp))

The expression following default must be in parentheses. This form is useful if you want to perform date arithmetic using SQLite date and time functions or modifiers.

5
  • Doug, parenth are only required if an expression is involved. The 'current_xxxx' are keywords and act as literal-value. sqlite.org/syntaxdiagrams.html#column-constraint Apr 11, 2010 at 0:23
  • Sky, yes, isn't that what I said? My point is that if you want more than what the keywords provide, you may use expressions, but the expression must be in parentheses; the create statement in the original question was missing the parentheses. Apr 11, 2010 at 22:10
  • 1
    There's no reason to write datetime(current_timestamp). Just use current_timestamp.
    – dan04
    Apr 12, 2010 at 5:38
  • 2
    Ah, perhaps I should have used a more complicated example, such as default (datetime('now','start of day')) Apr 12, 2010 at 15:24
  • 3
    DEFAULT (datetime('now','localtime')) is useful if you want the local time.
    – wimh
    Dec 20, 2013 at 8:39
8

CURRENT_TIMESTAMP is a literal-value just like 'mystring'

column-constraint:

enter image description here

literal-value:

enter image description here

1
  • Yes, and the datetime('now') the OP used is an expr, so it needs the parentheses.
    – TWiStErRob
    Apr 29, 2015 at 13:51
-1

you can use the following query for using current date value in your table

create table tablename (date_field_name Created_on default CURRENT_DATE);
1
  • off topic, original question is about a datetime field, not a date field. Jan 31, 2019 at 10:17

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.