考虑table1
和table2
。
table1
的{{1}}的值为Column1
'some_string Date: 00:30 05/16/2018 '
的{{1}}的值为table2
现在我需要同时更新Column2
和'some_string Date: 07-JUN-2018 '
,其中要求首先从给定的字符串值中找到日期,然后将该日期转换为格式'HH24:MI:ss MM / DD / YYYY '
在这里,问题是需要在不同的服务器上更新值,其中字符串值的日期可能具有不同的格式。我试图写上面的查询,但它不起作用。
table1
答案 0 :(得分:1)
Oracle 11g R2架构设置:
CREATE TABLE table1 ( column1 VARCHAR2(100) );
INSERT INTO table1 VALUES ( 'some_string Date: 00:30 05/16/2018 ' );
查询1 :
MERGE INTO table1 dst
USING (
WITH split ( rid, prefix, dt ) AS (
SELECT ROWID,
SUBSTR( column1, 1, INSTR( column1, 'Date: ' ) + 5 ),
SUBSTR( column1, INSTR( column1, 'Date: ' ) + 6 )
FROM table1
WHERE INSTR( column1, 'Date: ' ) > 0
),
converted_dts ( rid, prefix, dt ) AS (
SELECT rid,
prefix,
CASE
WHEN REGEXP_LIKE( dt, '^\s*\d?\d:\d\d \d?\d([/-])\d?\d\1\d{1,4}\s*$' )
THEN TO_DATE( dt, 'HH24:MI MM/DD/YYYY' )
WHEN REGEXP_LIKE( dt, '^\s*\d?\d([\-])(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NO|DEC)\1\d{1-4})\s*%' )
THEN TO_DATE( dt, 'DD-MON-YYYY' )
END
FROM split
)
SELECT rid,
prefix,
TO_CHAR( dt, 'HH24:MI:ss MM/DD/YYYY' ) AS dt
FROM converted_dts
WHERE dt IS NOT NULL
) src
ON ( dst.ROWID = src.RID )
WHEN MATCHED THEN
UPDATE
SET column1 = prefix || dt
Results :
查询2 :
SELECT *
FROM table1
Results :
| COLUMN1 |
|---------------------------------------|
| some_string Date: 00:30:00 05/16/2018 |