简体   繁体   English

试图在 Oracle SQL 中将 FULL_NAME 分成 FIRST_NAME 和 LAST_NAME,但它没有显示表本身的任何更改

[英]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.不需要他们。

Here is a db<>fiddle. 是一个 db<>fiddle。

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

相关问题 通过First_Name和/或Last_Name查询sql表 - query sql table By First_Name and/or Last_Name GROUP BY First_Name,但显示COUNT个对应的Last_Name SQL - GROUP BY First_Name but show COUNT of the corresponding Last_Name SQL 选择 concat(first_name,&#39; &#39;,last_name) 作为员工的“全名”---在 oracle 中连接 2 列时如何带空间--- - select concat(first_name,' ',last_name) as "full name "from employees ---how to bring space when 2 columns are concatenated in oracle--- 具有名称,名字和姓氏的PostgreSQL多表查询 - PostgreSQL multi-table query with name, first_name, and last_name 使用 Reg_exp 从电子邮件预言机中提取名字和姓氏 - Extracting First_name & Last_name from email oracle using Reg_exp 如何使用sql查询组合first_name和last_name? - How to combined first_name and last_name using sql query? 如有空格,如何将NAME分为3个不同的字段FIRST_NAME,MIDDLE_NAME和LAST_NAME - How to split NAME into 3 different fields FIRST_NAME, MIDDLE_NAME & LAST_NAME when there is a space 将 FULL_NAME 拆分为 LAST、FIRST 和 MIDDLE NAME - split FULL_NAME into LAST,FIRST & MIDDLE NAME 表示 Last_Name + First_Name 有一个具有特定值的记录 - Indicate a Last_Name + First_Name has one record with specific value 在本地数据库中存储 OAuth 用户信息(用户名、名字、姓氏等) - Storing OAuth user info (username, first_name, last_name etc) in local database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM