Oracle 将字符插入字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18230658/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 05:13:02  来源:igfitidea点击:

Oracle insert character into a string

oracle

提问by user1761160

I have this table of rows

我有这个行表

RowA
______
ABC123
DEF432
WER677
JKL342

how can I add a '_' in between the record using oracle to this? Assuming to add on the last 4 character.

如何使用 oracle 在记录之间添加“_”?假设添加最后 4 个字符。

RowA
______
ABC_123
DEF_432
WER_677
JKL_342

回答by M. Abbas

You would try something like:

你会尝试这样的事情:

Update Table_name set table_column = substr(table_column, 1, 3) || '_' || substr(table_column, 4);

The SUBSTRfunctions allows you to extract a substring from a string. The syntax for the SUBSTRfunction is:

这些SUBSTR函数允许您从字符串中提取子字符串。该SUBSTR函数的语法是:

SUBSTR( string, start_position, [ length ] )

stringis the source string.

string是源字符串。

start_positionis the position for extraction. The first position in the string is always 1.

start_position是提取位置。字符串中的第一个位置始终为 1。

lengthis optional. It is the number of characters to extract. If this parameter is omitted, the SUBSTR function will return the entire string.

length是可选的。它是要提取的字符数。如果省略此参数,SUBSTR 函数将返回整个字符串。

回答by Nick Krasnov

Another approach, using regexp_replace()regular expression function:

另一种方法,使用regexp_replace()正则表达式函数:

 select regexp_replace(RowA, '^([[:alpha:]]{3})', '_') as res
   from your_table

Result:

结果:

RES
----------
ABC_123
DEF_432
WER_677
JKL_342

SQLFiddle Demo

SQLFiddle 演示