我正在使用的表有这些奇怪的编码时间,我试图对它们进行格式化。我得到的错误是"缺少关键字"。 TRANS_NUMBER是我需要重新格式化的一串数字。
CASE POST_TIME.TRANS_NUMBER
WHEN '' THEN ''
WHEN NULL THEN ''
WHEN SUBSTR(POST_TIME.TRANS_NUMBER,10,1) =':' THEN CONCAT('0', SUBSTR(POST_TIME.TRANS_NUMBER,9,1))
ELSE SUBSTR(POST_TIME.TRANS_NUMBER,9,2)
END AS "POSTED_HOUR",
CASE POST_TIME.TRANS_NUMBER
WHEN '' THEN ''
WHEN NULL THEN ''
WHEN SUBSTR(POST_TIME.TRANS_NUMBER,12,1) =':' THEN CONCAT( CONCAT( '0', SUBSTR(POST_TIME.TRANS_NUMBER,11,1) ), CONCAT(' ', SUBSTR(POST_TIME.TRANS_NUMBER,13,2) ) )
ELSE CONCAT( CONCAT( '0', SUBSTR(POST_TIME.TRANS_NUMBER,11,2) ), CONCAT(' ', SUBSTR(POST_TIME.TRANS_NUMBER,13,2) ) )
END AS "POSTED_MINUTE"
答案 0 :(得分:2)
您正在混淆simple and searched CASE statements的语法,这就是您收到错误的原因。
您可以将其重写为:
CASE WHEN POST_TIME.TRANS_NUMBER IS NULL THEN NULL
WHEN SUBSTR(POST_TIME.TRANS_NUMBER,10,1) = ':' THEN '0'||SUBSTR(POST_TIME.TRANS_NUMBER,9,1)
ELSE SUBSTR(POST_TIME.TRANS_NUMBER,9,2)
END AS "POSTED_HOUR",
CASE WHEN POST_TIME.TRANS_NUMBER IS NULL THEN NULL
WHEN SUBSTR(POST_TIME.TRANS_NUMBER,12,1) =':' THEN '0'||SUBSTR(POST_TIME.TRANS_NUMBER,11,1)||' '||SUBSTR(POST_TIME.TRANS_NUMBER,13,2)
ELSE '0'||SUBSTR(POST_TIME.TRANS_NUMBER,11,2)||' '||SUBSTR(POST_TIME.TRANS_NUMBER,13,2)
END AS "POSTED_MINUTE"
N.B。我已经用更常见的CONCAT()
和(恕我直言)轻松阅读了||
此外,上述内容尚未经过测试,因为您没有提供任何示例数据供我们测试。
ETA:当POST_TIME.TRANS_NUMBER为空时,你甚至不需要显式处理这种情况,因为空值的SUBSTR()返回null。
答案 1 :(得分:1)
你的第3个应该是嵌套的案例:
CASE POST_TIME.TRANS_NUMBER
WHEN '' THEN ''
WHEN NULL THEN ''
else
case WHEN SUBSTR(POST_TIME.TRANS_NUMBER,10,1) =':' THEN CONCAT('0', SUBSTR(POST_TIME.TRANS_NUMBER,9,1))
ELSE SUBSTR(POST_TIME.TRANS_NUMBER,9,2)
end
END AS "POSTED_HOUR",