9

The year is given as int: 2009, 2010 etc.
I want to convert this information to DATE (first January).

My solutions (I prefer the first one):

(year::char || '-01-01')::DATE
'0001-01-01' + ((year-1)::char || ' year')::interval

Is there a better (build in) or more elegant and faster solution?
(I'm currently working with PostgreSQL 8.4 but are also interested in more recent versions.)

4
  • 2
    Your problem description is a bit loose: PostgreSQL (8.4) has no single-argument to_char() nor a TINYINT type.
    – pilcrow
    Commented Dec 28, 2011 at 15:24
  • 1
    String literals are single quoted '-01-01' in SQL. Double quotes are reserved for identifiers. Commented Dec 28, 2011 at 19:29
  • 1
    watch those quote characters! Single quotes for values, double quotes (only) for identifiers (parts of the schema: column names etc.)
    – araqnid
    Commented Dec 28, 2011 at 19:30
  • 1
    Thanks for the comments! Of course you are all right - I wasn't too careful typing the example. The mix-up of PostgreSQL versions is based on reading of different sources and different installations (local and remote).
    – FloE
    Commented Dec 28, 2011 at 23:41

4 Answers 4

16

I think this is the simplest way:

to_date(year::varchar, 'yyyy')
1
  • 1
    In PostgreSQL, text is preferable to varchar. And 'yyyy' should be 'YYYY' if I read the PostgreSQL manual correctly.
    – vog
    Commented Dec 12, 2016 at 9:38
4
SELECT to_date(2011::text, 'YYYY');

Attention: any code based on default casting from text to date is bad. Somebody can change a default format datestyle to some unexpected value, and this code fails. Using to_date or to_timestamp is very preferable. to_date or to_timestamp is relative expensive, but it is rock robust.

2
  • 1
    +1 for DateStyle caveat. However, in this particular example, is there any DateStyle input mode that does not correctly understand YYYY-MM-DD? (The docs suggest there is not.)
    – pilcrow
    Commented Dec 29, 2011 at 15:27
  • I hope so YYYY-MM-DD or YYYYMMDD will be parsed everywhere - but it is not ensured. Commented Dec 29, 2011 at 16:33
2
to_date('01 Jan ' || year, 'DD Mon YYYY')

OR

SELECT (DATE (year || '-01-01'))

ref: http://www.postgresql.org/docs/current/interactive/functions-formatting.html
Note: I haven't worked with PostgreSQL

1

One possibility:

select year * '1 year'::interval + '0000-01-01'::date;

I like this way because it avoids conversion between text and integer (once all the constants are parsed).

3
  • Interesting approach. However it gives ERROR: date/time field value out of range: "0000-01-01" under pg 9.1.
    – pilcrow
    Commented Dec 29, 2011 at 15:13
  • Also, as a trivial observation, this recipe will return a TIMESTAMP WITHOUT TIME ZONE rather than a DATE, though I suspect that matters not at all in the common case.
    – pilcrow
    Commented Dec 29, 2011 at 15:19
  • ah, I was testing it on a pg8.3 instance. Maybe this depends on if you build with floating-point vs int times?
    – araqnid
    Commented Dec 29, 2011 at 19:23

Your Answer

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

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