Oracle:将 DD-MM-YYYY 转换为 MON-YYYY
技术问答
445 人阅读
|
0 人回复
|
2023-09-12
|
以下代码SDate ='10-04-2020'格式为 DD-MM-YYYY,我只需要把月份和年份和给定的 MON-YYYY 比较日期1 h$ B8 \5 `+ q
SELECT WR.EmpID AS EMPSID,SUM(WR.ESALARY) AS TOTAL_SALARYFROM WEEKLY_REPORT WRWHERE WR.EmpID = 'E009' AND TO_CHAR(WR.SDate,'MON-YYYY') = TO_CHAR('APR-2020','MON-YYYY') GROUP BY WR.EmpID;错误 ORA-01722: invalid number
6 U) [( N, M) M" Z* \
2 B% w2 O% h2 }4 L& } 解决方案:
# Z; ]. s, h6 o 因为你的日期是 dd-mm-yyyy 格式,所以你需要to_date(WR.SDate,'dd-mm-yyyy使用 to_char() 以前用过。而不是 TO_CHAR('APR-2020','MON-YYYY')使用'APR-这是一个有效的字符串。1 u4 h* O: P$ g" M1 w8 C
SELECT WR.EmpID AS EMPSID,SUM(WR.ESALARY) AS TOTAL_SALARYFROM WEEKLY_REPORT WRWHERE WR.EmpID = 'E009' AND TO_CHAR(to_date(WR.SDate,'dd-mm-yyyy'),'MON-YYYY') = 'APR-2020' GROUP BY WR.EmpID;数据库小提琴:5 ]& V) N% V9 E) o
将日期转换为 mon-yyyy 的查询:' d* ^ {. o8 y( g8 r9 h- H
select TO_CHAR(to_date('10-04-2020','dd-mm-yyyy'),'MON-YYYY') result from dual输出:. C* U6 ]8 d a3 ]& P# q/ r$ E
结果& T5 s1 i9 g* D# j, k8 u! `
2020 年 4 月5 @/ W8 d6 p0 _4 ~
查询检查条件:+ ?/ A. i9 R1 N5 E+ y
select 'true' result from dual where TO_CHAR(to_date('10-04-2020','dd-mm-yyyy'),'MON-YYYY') = 'APR-2020'输出:
4 N( N& J% F/ p% I; A5 W结果+ x6 w: x8 _9 d! D) G4 I1 t
true |
|
|
|
|
|