SELECT warranty_period,
CASE warranty_period
WHEN INTERVAL '0-0' YEAR TO MONTH
THEN 'No warranty'
ELSE TO_CHAR(warranty_period)
END AS descr
FROM table_name
其中,对于示例数据:
CREATE TABLE table_name (warranty_period) AS
SELECT INTERVAL '0-0' YEAR TO MONTH FROM DUAL UNION ALL
SELECT INTERVAL '0-1' YEAR TO MONTH FROM DUAL UNION ALL
SELECT INTERVAL '1-0' YEAR TO MONTH FROM DUAL UNION ALL
SELECT INTERVAL '1-2' YEAR TO MONTH FROM DUAL;
SELECT warranty_period,
CASE warranty_period
WHEN INTERVAL '0-0' YEAR TO MONTH
THEN 'No warranty'
ELSE EXTRACT(YEAR FROM warranty_period) || 'y ' || EXTRACT(MONTH FROM warranty_period) || 'm'
END AS descr
FROM table_name
SELECT warranty_period,
CASE
WHEN warranty_period <= INTERVAL '0-0' YEAR TO MONTH
THEN 'No warranty'
WHEN warranty_period < INTERVAL '1-0' YEAR TO MONTH
THEN EXTRACT(MONTH FROM warranty_period) || ' months'
WHEN EXTRACT(MONTH FROM warranty_period) = 0
THEN EXTRACT(YEAR FROM warranty_period) || ' years'
ELSE EXTRACT(YEAR FROM warranty_period) || ' years and ' || EXTRACT(MONTH FROM warranty_period) || ' months'
END AS descr
FROM table_name
1条答案
按热度按时间kwvwclae1#
使用
CASE
表达式:其中,对于示例数据:
输出:
| 保修期|描述|
| - -|- -|
| +00-00|无保修|
| +00-01| +00-01|
| +01-00| +01-00|
| +1 -2| +1 -2|
或者,如果您想要不同的格式,则
EXTRACT
组件部分:输出:
| 保修期|描述|
| - -|- -|
| +00-00|无保修|
| +00-01| 0年1个月|
| +01-00| 1年0月|
| +1 -2| 1年2个月|
或:
输出:
| 保修期|描述|
| - -|- -|
| +00-00|无保修|
| +00-01| 1个月|
| +01-00| 1年|
| +1 -2| 1年零2个月|
fiddle