例如:一行中的一行包含以下数据。我需要将UNIX时间戳转换为oracle中的实际日期时间。请提供sql查询以进行转换。 注意:{EOT}是特殊字符“传输结束”。 {ETX}是特殊字符“文本结尾”。
1550226213{EOT}Bharath
testtest{ETX}1550226559{EOT}LakshmanUpdate to Current Summary: {EOT}Under Investigation{EOT}
suresh{ETX}1550227918S{EOT}itaUpdate to Current Summary: {EOT}Outage restored- Under Observation
{ETX}1550301176{EOT}Rama
答案 0 :(得分:1)
假设所有时间都在字符串中每行的开头,那么您可以使用递归子查询因数子句遍历字符串中的每个子字符串行,并使用正则表达式在字符串的开头查找每次那行,然后您要做的就是add the offset interval to the epoch的时间(从1970-01-01开始):
Oracle设置:
CREATE TABLE test_data ( id, value ) AS
SELECT 1, '1550226213Bharath
1550226559LakshmanUpdate to Current Summary: Under Investigation
1550227918SitaUpdate to Current Summary: Outage restored- Under Observation
1550301176Rama' FROM DUAL UNION ALL
SELECT 2, '0ABC' FROM DUAL UNION ALL
SELECT 3, NULL FROM DUAL UNION ALL
SELECT 4, '1234567890A
1234567891B
1234567892C' FROM DUAL;
查询:
WITH lines ( id, value, unix_time, description, line_no, total_lines ) AS (
SELECT id,
value,
TO_NUMBER( REGEXP_SUBSTR( value, '^\d+', 1, 1, 'm' ) ),
REGEXP_SUBSTR( value, '^\d+(.*)$', 1, 1, 'm', 1 ),
1,
COALESCE( REGEXP_COUNT( value, '^\d+', 1, 'm' ), 0 )
FROM test_data
UNION ALL
SELECT id,
value,
TO_NUMBER( REGEXP_SUBSTR( value, '^\d+', 1, line_no + 1, 'm' ) ),
REGEXP_SUBSTR( value, '^\d+(.*)$', 1, line_no + 1, 'm', 1 ),
line_no + 1,
total_lines
FROM lines
WHERE line_no < total_lines
)
SELECT id,
DATE '1970-01-01' + unix_time * INTERVAL '1' SECOND AS time,
description
FROM lines
ORDER BY id, line_no;
输出:
ID | TIME | DESCRIPTION -: | :------------------ | :---------------------------------------------------------------- 1 | 2019-02-15 10:23:33 | Bharath 1 | 2019-02-15 10:29:19 | LakshmanUpdate to Current Summary: Under Investigation 1 | 2019-02-15 10:51:58 | SitaUpdate to Current Summary: Outage restored- Under Observation 1 | 2019-02-16 07:12:56 | Rama 2 | 1970-01-01 00:00:00 | ABC 3 | null | null 4 | 2009-02-13 23:31:30 | A 4 | 2009-02-13 23:31:31 | B 4 | 2009-02-13 23:31:32 | C
db <>提琴here
更新:
Oracle设置:
CREATE TABLE test_data ( id, value ) AS
SELECT 1, '1550226213{EOT}Bharath
testtest{ETX}1550226559{EOT}LakshmanUpdate to Current Summary: {EOT}Under Investigation{EOT}
suresh{ETX}1550227918{EOT}itaUpdate to Current Summary: {EOT}Outage restored- Under Observation
{ETX}1550301176{EOT}Rama' FROM DUAL UNION ALL
SELECT 2, '0{EOT}ABC' FROM DUAL UNION ALL
SELECT 3, NULL FROM DUAL UNION ALL
SELECT 4, '1234567890{EOT}A{ETX}1234567891{EOT}B{ETX}1234567892{EOT}C' FROM DUAL;
查询:
WITH lines ( id, value, line, line_no, total_lines ) AS (
SELECT id,
value,
REGEXP_SUBSTR( value, '(.+?)(\{ETX\}|$)', 1, 1, 'n', 1 ),
1,
COALESCE( REGEXP_COUNT( value, '(.+?)(\{ETX\}|$)', 1, 'n' ), 0 )
FROM test_data
UNION ALL
SELECT id,
value,
REGEXP_SUBSTR( value, '(.+?)(\{ETX\}|$)', 1, line_no + 1, 'n', 1 ),
line_no + 1,
total_lines
FROM lines
WHERE line_no < total_lines
)
SELECT id,
DATE '1970-01-01' + TO_NUMBER( REGEXP_SUBSTR( line, '^(\d+)(\{EOT\}|$)', 1, 1, 'n', 1 ) ) * INTERVAL '1' SECOND AS time,
REGEXP_SUBSTR( line, '^(\d+)\{EOT\}(.*)$', 1, 1, 'n', 2 ) AS description
FROM lines
ORDER BY id, line_no;
输出:
ID | TIME | DESCRIPTION -: | :------------------ | :------------------------------------------------------------------------- 1 | 2019-02-15 10:23:33 | Bharath<br>testtest 1 | 2019-02-15 10:29:19 | LakshmanUpdate to Current Summary: {EOT}Under Investigation{EOT}<br>suresh 1 | 2019-02-15 10:51:58 | itaUpdate to Current Summary: {EOT}Outage restored- Under Observation<br> 1 | 2019-02-16 07:12:56 | Rama 2 | 1970-01-01 00:00:00 | ABC 3 | null | null 4 | 2009-02-13 23:31:30 | A 4 | 2009-02-13 23:31:31 | B 4 | 2009-02-13 23:31:32 | C
db <>提琴here