[英]Add a column based on calculation from other columns to an existing table in SQL Server
I know how to add a column in a table via the following code我知道如何通过以下代码在表中添加一列
ALTER TABLE {TABLENAME}
ADD {COLUMNNAME} {TYPE} {NULL|NOT NULL}
CONSTRAINT {CONSTRAINT_NAME} DEFAULT {DEFAULT_VALUE}
WITH VALUES
My question is how to fit in what I want to be the new AGE column in my table which is calculated based on the birth dates I got which looks like this:我的问题是如何适应我想要成为表格中新 AGE 列的内容,该列是根据我得到的出生日期计算的,如下所示:
to_number((CURRENT_DATE()-to_date(date_of_birth,'dd/mm/yyyy'))/365)
How does this work in SQL
?这在
SQL
如何工作的?
Why don't you create a view for this table, and add this expression as a column to your view?你为什么不为这个表创建一个视图,并将这个表达式作为列添加到你的视图中?
create table friends (id number, name varchar, date_of_birth varchar);
insert into friends values ( 1, 'Gokhan', '01/01/1980');
create view friends_v as select id, name, date_of_birth, to_number((CURRENT_DATE()-to_date(date_of_birth,'dd/mm/yyyy'))/365) as age from friends;
select * from friends_v;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.