sql - Case expression for date format conversion
问题描述
I have an Entrydate
column that comes from Source in below date formats
- YYYY-MM-DD HH.MM.SS
- YYYY-MM-DD
It is loaded to a table as YYYYMM
format. I am looking for a case expression that handles both the conditions.
I am using the below SQL
SELECT
CASE
WHEN to_char(ENTRYDATE, 'YYYY-MM-DD HH.MM.SS')
THEN to_char(ENTRYDATE, 'YYYYMM')
to_char(ENTRYDATE, 'YYYY-MM-DD')
THEN to_char(ENTRYDATE, 'YYYYMM')
FROM
TABLE;
but I get this error:
ORA-00920: invalid relational operator
I will need to get the date in YYYYMM format to validate the output table.
解决方案
您得到的原因invalid relational operator
是该函数TO_CHAR()
不返回布尔值。
我的猜测是,您将表面上的日期值存储为CHAR
or VARCHAR
,并且您需要转换为某种格式。如果它们被存储为DATE
值,那么你应该没什么可做的。但假设CHAR
// VARCHAR
:VARCHAR2
SELECT CASE WHEN REGEXP_LIKE(entrydate, '^\d{4}-\d{2}-\d{2}( \d{2}\.\d{2}\.\d{2})$')
THEN REGEXP_REPLACE(entrydate, '^(\d{4})-(\d{2}).*$', '\1\2')
END AS yyyymm_date
FROM table;
(请注意,您的陈述中也缺少一个END
。CASE
)
NULL
如果 的值entrydate
是其他格式,则上面将返回 a 。
希望这可以帮助。
推荐阅读
- java - 我想要如图所示的轮廓文本字段,未单击edittext或微调器我想要与按钮单击上的视图相同
- html - 将 Boostrap 和纯 HTML CSS 文件合并到 wordpress 主题中?
- python - 导入中的 Pandas 和循环引用问题
- amazon-web-services - Terraform RKE 提供者与 Rancher2 提供者
- linux - 使用 FIFO 进行进程间通信
- unity3d - 如何在运行时更改材质颜色和发射颜色
- snowflake-cloud-data-platform - Snowflake JDBC TIME 数据类型值转换为 UTC
- mysql - Docker容器无法连接到主机上运行的mysql
- c - 数据结构和指针 - C 代码解释
- entity-framework - 即使表未更改,也将 AddColumn 添加到迁移中