Re: How can I replace the year of the created_at column with the current year dynamically ?

Поиск
Список
Период
Сортировка
От Steve Crawford
Тема Re: How can I replace the year of the created_at column with the current year dynamically ?
Дата
Msg-id 53B42873.3070105@pinpointresearch.com
обсуждение исходный текст
Ответ на How can I replace the year of the created_at column with the current year dynamically ?  (Arup Rakshit <aruprakshit@rocketmail.com>)
Ответы Re: How can I replace the year of the created_at column with the current year dynamically ?  (Arup Rakshit <aruprakshit@rocketmail.com>)
Список pgsql-general
On 07/01/2014 11:27 PM, Arup Rakshit wrote:
Here is my try :

staging::=> select  to_char(created_at,'DD/MM') || '/' || to_char(now(),'YYYY') as when from users;
    when    
------------
 24/02/2014
...
 20/02/2014
(15 rows)

Can the same be done using any other clever trick ? 


No tricks are springing to mind but a warning is. The above will produce illegal dates whenever you are an inconvenient number of years past February 29. I think this will fix that issue:

select created_at + ((extract(year from now()) - extract(year from created_at)) * '1 year'::interval);

Note that the above returns a date (assuming that created_at is a date). You may need to apply to_char to format to your desired specification.

Cheers,
Steve

В списке pgsql-general по дате отправления:

Предыдущее
От: sunpeng
Дата:
Сообщение: Re: Migration error " invalid byte sequence for encoding "UTF8": 0xff " from mysql 5.5 to postgresql 9.1
Следующее
От: Adrian Klaver
Дата:
Сообщение: Re: Question About Roles