[英]Oracle SQL - select parts of a string
How can I select abcdef.txt
from the following string? 如何从以下字符串中选择
abcdef.txt
?
abcdef.123.txt
I only know how to select abcdef by doing select substr('abcdef.123.txt',1,6) from dual;
我只知道如何通过
select substr('abcdef.123.txt',1,6) from dual;
来选择abcdef select substr('abcdef.123.txt',1,6) from dual;
You can using || 您可以使用|| for concat and substr -3 for right part
对于concat和substr -3对于右部分
select substr('abcdef.123.txt',1,6) || '.' ||substr('abcdef.123.txt',-3) from dual;
or avoiding a concat (like suggested by Luc M) 或避免连任(如Luc M所建议)
select substr('abcdef.123.txt',1,7) || substr('abcdef.123.txt',-3) from dual;
A general solution, assuming the input string has exactly two periods .
一个通用的解决方案,假设输入字符串恰好有两个句点
.
and you want to extract the first and third tokens, separated by one .
并且您要提取第一个和第三个标记,以一个分隔
.
The length of the "tokens" in the input string can be arbitrary (including zero!) and they can contain any characters other than .
输入字符串中“令牌”的长度可以是任意的(包括零!),并且可以包含除以外的任何字符
.
select regexp_replace('abcde.123.xyz', '([^.]*).([^.]*).([^.]*)', '\1.\3') as result
from dual;
RESULT
---------
abcde.xyz
Explanation: 说明:
[ ]
means match any of the characters between brackets . [ ]
表示匹配方括号之间的任何字符 。 ^
means do NOT match the characters in the brackets - so... ^
表示不匹配括号中的字符 -因此... [^.]
means match any character OTHER THAN .
[^.]
表示匹配除以外的任何字符.
*
means match zero or more occurrences, as many as possible ( "greedy" match) *
表示匹配零次或多次出现,并尽可能多 ( “贪婪”匹配) ( ... )
is called a subexpression ... see below ( ... )
被称为子表达式 ...见下文 '\\1.\\3
means replace the original string with the first subexpression, followed by .
'\\1.\\3
表示将原始字符串替换为第一个子表达式,然后是.
, followed by the THIRD subexpression . Replace the substring of anything surrounded by dots (inclusive) with a single dot. 用单个点替换点(包括点)包围的所有子字符串。 No dependence on lengths of components of the string:
不依赖于字符串的组成部分的长度:
SQL> select regexp_replace('abcdef.123.txt', '\..*\.', '.') fixed
from dual;
FIXED
----------
abcdef.txt
SQL>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.