oracle 在 SQL 更新语句中使用子字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39780543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using substring in SQL update statement
提问by user3266259
I'm writing a SQL query that updates the CUST_NAME column by appending the the word "CHECKED to it. The column is only 100 characters long however and I'm going to run into errors if I have an already long customer name and try appending CHECKED to it. So I want to use the Substring function in sql, but kind of stumped. I want to be able to substring the CUST_NAME field if it will go over 100 characters with the appended word. How can I do that? Thank you
我正在编写一个 SQL 查询,通过将单词“CHECKED”附加到它来更新 CUST_NAME 列。但是,该列只有 100 个字符长,如果我的客户名称已经很长,我将遇到错误并尝试附加检查它。所以我想在 sql 中使用 Substring 函数,但有点难。如果 CUST_NAME 字段的附加单词超过 100 个字符,我希望能够对它进行子字符串化。我该怎么做?谢谢
UPDATE CUST_INFO cust
SET CUST_NAME = (CUST_NAME||'_CHECKED')
WHERE process_timestamp = null;
回答by Gordon Linoff
Here is one way:
这是一种方法:
UPDATE CUST_INFO cust
SET CUST_NAME = SUBSTR(CUST_NAME, 1, 92) || '_CHECKED'
WHERE process_timestamp is null;
Also, if you want to update any records, then use is null
rather than = null
(the latter never evaluates to true).
此外,如果您想更新任何记录,请使用is null
而不是= null
(后者永远不会评估为真)。
Note: Not all databases have the left()
function, you can use substr()
or the equivalent instead.
注意:不是所有的数据库都有这个left()
功能,你可以用substr()
或 等效的来代替。