Re: Extract from date field - Mailing list pgsql-novice

From Thom Brown
Subject Re: Extract from date field
Date
Msg-id BANLkTinkkOpV76K3bwyRYSczAqD0WkGLng@mail.gmail.com
Whole thread Raw
In response to Re: Extract from date field  (James David Smith <james.david.smith@gmail.com>)
Responses Re: Extract from date field  (James David Smith <james.david.smith@gmail.com>)
Re: Extract from date field  (Thom Brown <thom@linux.com>)
List pgsql-novice
On 17 June 2011 20:12, James David Smith <james.david.smith@gmail.com> wrote:
> Hey,
>
> I realise it's probably poor form to reply to your own question, but I
> thought I'd just say I've managed to do this! Well, question 2 anyway.
> To extract a DAY from a date field, and keep a leading zero, I have
> used the 'lpad' function like so:
>
> SELECT lpad (cast((EXTRACT(DAY from timestamp '2010-01-01')) as
> varchar), 2, '0')
>
> Except that in my query it looks like this:
>
> (SELECT lpad (cast((EXTRACT(DAY from date_of_incident)) as varchar), 2, '0'))
>
> I thought I'd post this incase it's off use to anyone else.
>
> Just need to figure out how to convert it all to a integer now...

Ah, I believe I've replied to this on Twitter, but I'll post here too.
 You can use the very handy to_char function to convert your date into
a formatted string. (see
http://www.postgresql.org/docs/9.0/static/functions-formatting.html )
So you'd end up with:

ALTER TABLE camdengps2
ADD COLUMN camdencrimes_link varchar;
UPDATE camdengps2
SET camdencrimes_link=(to_char(date_time, 'DDMMYYYY') || incident)::int

Hope that solves it for you.

--
Thom Brown
Twitter: @darkixion
IRC (freenode): dark_ixion
Registered Linux user: #516935

EnterpriseDB UK: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

pgsql-novice by date:

Previous
From: James David Smith
Date:
Subject: Re: Extract from date field
Next
From: LALIT KUMAR
Date:
Subject: Comparing dates in a pattern