[英]Trying to separate a FULL_NAME into FIRST_NAME and LAST_NAME in Oracle SQL, but it does not show any changes on the table itself
This code is supposed to separate full name and it does.此代码应该分隔全名,并且确实如此。 This code works fine, except for the fact that it does not add the data to the table.这段代码工作正常,除了它没有将数据添加到表中。
SELECT FULL_NAME
SUBSTR(FULL_NAME, 1, INSTR(FULL_NAME, ' ', 1, 1)-1) as FIRST_NAME,
SUBSTR(FULL_NAME, INSTR(FULL_NAME, ' ', -1)+1) as LAST_NAME
from customers;
This code runs similarly but again, does not populate the table.此代码以类似方式运行,但同样不会填充表。
ALTER TABLE CUSTOMERS;
SELECT
substr("A1"."FULL_NAME", 1, instr("A1"."FULL_NAME", ' ', 1, 1) - 1) "FIRST_NAME",
substr("A1"."FULL_NAME", instr("A1"."FULL_NAME", ' ',(- 1)) + 1) "LAST_NAME"
FROM
"XXXX"."CUSTOMERS" "A1";
This is what my query result is, it shows that everything is how it should be.这就是我的查询结果,它表明一切都是应该的。
This is what my table looks like but as you can see, the last two columns do not have any data.这是我的表格的样子,但如您所见,最后两列没有任何数据。
My goal is to be able to drop the FULL_NAME column, but I need these two split first and I am not sure why it is not working.我的目标是能够删除 FULL_NAME 列,但我需要先将这两个拆分,我不确定它为什么不起作用。 Any help is appreciated.任何帮助表示赞赏。
A SELECT
statement only displays data. SELECT
语句仅显示数据。 It does not change it.它不会改变它。 ALTER TABLE
is a DDL command that would actually change the structure of the table (like add a column), but you have not told it what to do; ALTER TABLE
是一个 DDL 命令,它实际上会改变表的结构(比如添加一列),但你还没有告诉它要做什么; it does not change data.它不会更改数据。 Use UPDATE
to change data values:使用UPDATE
更改数据值:
UPDATE CUSTOMERS SET
FIRST_NAME = SUBSTR(FULL_NAME, 1, INSTR(FULL_NAME, ' ', 1, 1)-1),
LAST_NAME = SUBSTR(FULL_NAME, INSTR(FULL_NAME, ' ', -1)+1);
COMMIT;
You can use generated columns:您可以使用生成的列:
ALTER TABLE CUSTOMERS
ADD FIRST_NAME AS substr("FULL_NAME", 1, instr("FULL_NAME", ' ', 1, 1) - 1);
ALTER TABLE CUSTOMERS
ADD LAST_NAME AS substr("FULL_NAME", instr("FULL_NAME", ' ',(- 1)) + 1) ;
These are calculated when they are references so the values are always up-to-date.这些是在引用时计算的,因此这些值始终是最新的。 No UPDATE
is needed.不需要UPDATE
。
Note: I strongly, strongly discourage you from using double quotes for column names.注意:我强烈建议您不要在列名中使用双引号。 There is no need for them.不需要他们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.