telePG怎么字符串截取?

telePG怎么字符串截取?

例现在想截取  /dir/test/1.txt 中的test内容,要怎么截取?

也就是说想截取倒数第一个 / 至倒数第二个 / 中间的内容

SELECT SUBSTRING('/dir/test/1.txt' FROM '/dir/' FOR 'test') as test_dir;

substr('/dir/test/1.txt', 4, 4) 第一个4表示起始位置,第二个4表示截取长度。。请采纳哦!!

你可以使用 Teleport's built-in string manipulation functions 中的 slice 函数来实现字符串截取。

该函数的语法如下:

slice(string, start, end)


其中,string 为要截取的字符串,start 为开始截取的位置(从 0 开始计数),end 为结束截取的位置。

如果要截取 /dir/test/1.txt 中的 test 内容,可以这样使用 slice 函数:

result = slice("/dir/test/1.txt", 5, 8)


这样,result 变量中就会保存截取的字符串 "test"。

提供实例方法【PostgreSQL Substring字符串截取函数】,链接:https://huaweicloud.csdn.net/63355d6ed3efff3090b543dd.html?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~activity-7-125101563-blog-121750911.pc_relevant_3mothn_strategy_and_data_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~activity-7-125101563-blog-121750911.pc_relevant_3mothn_strategy_and_data_recovery&utm_relevant_index=9

路径1截取:

SELECT split_part('/dir/test/1.txt', '/', 3);

路径2截取:

SELECT split_part('/1/dir/test/1.txt', '/', 4);

split_part() 函数按照指定的分隔符将字符串分成多个部分,并返回指定位置的一个部分。

在 TelePG 中,你可以使用substr()函数来截取字符串的一部分。substr()函数的语法如下:

substr(string, start_index, length)

其中,string表示要截取的字符串,start_index表示要截取的字符串的起始位置,length表示要截取的字符串的长度。

因此,你可以使用以下代码来截取 /dir/test/1.txt 中的test内容:


substr("/dir/test/1.txt", 5, 4)

该代码将截取 /dir/test/1.txt 字符串中从第5个字符开始的4个字符,即test。

SQL:

select reverse(split_part(reverse('/dir/test/1.txt'), '/', 2)) 

结果:

reverse|
-------+
test   |