Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 向现有字符串中添加字符_String_Postgresql_Insert_Sql Update_Substring - Fatal编程技术网

String 向现有字符串中添加字符

String 向现有字符串中添加字符,string,postgresql,insert,sql-update,substring,String,Postgresql,Insert,Sql Update,Substring,对不起,如果我错过了一个可能的解决我的问题的办法,但我需要一些指针。我确信有一个简单的解决方案,但我(目前)没有看到 因此,我有一个Postgres DB表,它需要大量更新,只需在字符串中的特定位置添加一个字符 例如: 选择PINFO.title,SUBSTRING(PINFO.title FROM',\(.+).del')作为my\u表中的ep\u num,其中my\u条件… 所以我得到了这样的结果: +--------------------+--------+ | title

对不起,如果我错过了一个可能的解决我的问题的办法,但我需要一些指针。我确信有一个简单的解决方案,但我(目前)没有看到

因此,我有一个Postgres DB表,它需要大量更新,只需在字符串中的特定位置添加一个字符

例如:

选择PINFO.title,SUBSTRING(PINFO.title FROM',\(.+).del')作为my\u表中的ep\u num,其中my\u条件…

所以我得到了这样的结果:

+--------------------+--------+
|       title        | ep_num |
+--------------------+--------+
| my_series, 01. del |     01 |
| my_series, 10. del |     10 |
| my_series, 11. del |     11 |
| my_series, 2. del  |      2 |
| my_series, 3. del  |      3 |
| my_series, 4. del  |      4 |
| my_series, 5. del  |      5 |
| my_series, 6. del  |      6 |
| my_series, 7. del  |      7 |
| my_series, 8. del  |      8 |
| my_series, 9. del  |      9 |
+--------------------+--------+
所以我想在数字小于10的地方加一个0。因此,我的预期结果应该是:

+--------------------+--------+
|       title        | ep_num |
+--------------------+--------+
| my_series, 01. del |     01 |
| my_series, 10. del |     10 |
| my_series, 11. del |     11 |
| my_series, 02. del |     02 |
| my_series, 03. del |     03 |
| my_series, 04. del |     04 |
| my_series, 05. del |     05 |
| my_series, 06. del |     06 |
| my_series, 07. del |     07 |
| my_series, 08. del |     08 |
| my_series, 09. del |     09 |
+--------------------+--------+
最好的方法是什么?我需要更新表,而不仅仅是以这种方式打印输出。 非常感谢。 M.

使用函数


ep_num

regexp\u split\u to\u table()
将空格处的标题拆分为三部分。 他们可以合并后,再次左填充第二块。这里的
lpad()
函数创建一个3字符长的文本,而不是2字符长的文本,因为前面的拆分也与点有关。

使用函数


ep_num

regexp\u split\u to\u table()
将空格处的标题拆分为三部分。
他们可以合并后,再次左填充第二块。这里的
lpad()
函数创建一个3个字符长的文本,而不是2个字符长的文本,因为前面的拆分也涉及点。

忽略ep_num列,因为它只是额外显示的,便于查看和排序。错误是“错误:列“ep_num”不存在”。忽略ep_num列,因为它只是额外显示的,以便于查看和排序。错误为“错误:列“ep_num”不存在”。
UPDATE my_table
SET 
    title = 'my_series, ' || lpad(ep_num, 2, '0') || '. del',
    ep_num = lpad(ep_num, 2, '0')
UPDATE my_table mt
SET title = parts[1] || ' ' || lpad(parts[2], 3, '0') || ' ' || parts[3]
FROM (
    SELECT 
        title,
        regexp_split_to_array(title, ' ') AS parts
    FROM
        my_table
) s
WHERE mt.title = s.title