请帮助我进行此oracle sql查询

时间:2019-06-12 16:51:42

标签: sql oracle oracle11g

我必须编写一个oracle SQL查询,其中必须检查字符串的长度是否为64(如该字符串中那样为64) “ Lst021_23-FehlerDatenprotokoll.AenDienst2019.06.11_08.48.42.tx”,则字符串的扩展名应为txt,并且我们必须删除字符串的一部分,直到-

所以对于字符串
'Lst021_23-FehlerDatenprotokoll.AenDienst2019.06.11_08.48.42.tx'的输出将是

FehlerDatenprotokoll.AenDienst2019.06.11_08.48.42.txt

我正在尝试编写此查询

SELECT 'Lst021_23-Fehler-Datenprotokoll.AenDienst.2019.06.11_08.48.42.tx'FROM Dual;
CASE 
WHEN LENGTH('Lst021_23-Fehler-Datenprotokoll.AenDienst.2019.06.11_08.48.42.tx')=64 THEN CONCAT('SUBSTR('Lst021_23-Fehler-Datenprotokoll.AenDienst.2019.06.11_08.48.42.tx',11,53)','t')
END
CASE;

3 个答案:

答案 0 :(得分:0)

脚本中存在语法问题。尝试这种方式

SELECT 
CASE 
    WHEN LENGTH('Lst021_23-Fehler-Datenprotokoll.AenDienst.2019.06.11_08.48.42.tx') = 64
        THEN CONCAT(SUBSTR('Lst021_23-Fehler-Datenprotokoll.AenDienst.2019.06.11_08.48.42.tx',11,53),'t')
    ELSE 'Lst021_23-Fehler-Datenprotokoll.AenDienst.2019.06.11_08.48.42.tx'
END
FROM Dual;

答案 1 :(得分:0)

最好只写一次字符串:

with t( str ) as
(
 select 'Lst021_23-Fehler-Datenprotokoll.AenDienst.2019.06.11_08.48.42.tx' from dual   
)    
select decode(length(str), 64, concat(str,'t'))  as "File Name"
  from t;

File Name
-----------------------------------------------------------------
Lst021_23-Fehler-Datenprotokoll.AenDienst.2019.06.11_08.48.42.txt

Demo

答案 2 :(得分:0)

这个怎么样?如果长度为64,则:

  • 从第一个连字符(-)到字符串末尾取一个子字符串
  • 应用regexp_replace并用\w+$替换最后一个“单词”(txt)(正如您所说的扩展名必须为txt

我们在这里:

SQL> with test (id, col) as
  2    (select 1, 'Lst021_23-Fehler-Datenprotokoll.AenDienst.2019.06.11_08.48.42.tx' from dual
  3     union all
  4     select 2, 'blabla-Something-else.Which_is_64_characters_long.2019.06.12.txt' from dual
  5     union all
  6     select 3, 'This will not be "fixed" as it is shorter than 64 chars.exe'      from dual
  7     union all
  8     select 4, 'Yet another nice string-Kein_Fehler.Mitwoch.2019.06.13_22.22.com' from dual
  9     )
 10  select
 11    id,
 12    case when length(col) = 64 then
 13              regexp_replace(substr(col, instr(col, '-') + 1), '\w+$', 'txt')
 14         else col
 15    end result
 16  from test;

 ID RESULT
--- -----------------------------------------------------------------
  1 Fehler-Datenprotokoll.AenDienst.2019.06.11_08.48.42.txt
  2 Something-else.Which_is_64_characters_long.2019.06.12.txt
  3 This will not be "fixed" as it is shorter than 64 chars.exe
  4 Kein_Fehler.Mitwoch.2019.06.13_22.22.txt

SQL>