Omer Qureshi
Omer Qureshi

Reputation: 113

adding date column in postgresql

I have a column of order_date in a table named dates:

order_date
"2011-02-01"
"2012-03-23"
"2011-01-01"
"2011-07-04"

I'm trying to extract the dates and add it to a new column. I used the formula

select EXTRACT(day from order_date) 
from dates

to get the dates from the column.

Now to add the values in another column in I tried the following:

update dates 
   set date1 = select EXTRACT(day from order_date) from dates;

But unfortunately the above code is not working and gives me an error at the 'select' part.

Upvotes: 0

Views: 1559

Answers (1)

user330315
user330315

Reputation:

Get rid of the select

update dates 
   set date1 = EXTRACT(day from order_date);

But this seems rather useless. As a general rule you should not store information that can be derived from existing data. The overhead of extracting the day from a date is so small that storing that in another column really does not make sense.

Online example: http://rextester.com/NPONE96895

Upvotes: 1

Related Questions