简体   繁体   English

在PostgreSQL中添加日期列

[英]adding date column in postgresql

I have a column of order_date in a table named dates: 我在名为dates的表中有一列order_date:

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. 但是不幸的是,以上代码无法正常工作,在“选择”部分给了我一个错误。

Get rid of the select 摆脱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 在线示例: http : //rextester.com/NPONE96895

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM