2

I want to create a table with default value set to the current year

CREATE TABLE Date
(
 "year" smallint NOT NULL DEFAULT extract(year from current_time)
);

It throws an error:

!42000!syntax error, unexpected YEAR in: "select year"

The same command if I run using mclient it works fine.

sql>select extract(year from current_date);
+---------------------+
| second_current_date |
+=====================+
|              2015 |
+---------------------+
1 tuple (0.130ms)
sql>

sql>select "year"(NOW());
+-------------------+
| current_timestamp |
+===================+
|              2015 |
+-------------------+
1 tuple (0.380ms)

I only want the year. Is there any way to do it?

1 Answers1

0

It looks like you are attempting to extract 'year' from CURRENT_TIME when you should be attempting to get it from CURRENT_DATE

Try:

CREATE TABLE Date
(
     "year" smallint NOT NULL DEFAULT extract(year from current_date)
);
stinkyfriend
  • 926
  • 7
  • 8