以下代码SDate ='10-04-2020'格式为 DD-MM-YYYY,我只需要将月份和年份与给定的 MON-YYYY 日期进行比较
SDate ='10-04-2020'
DD-MM-YYYY
MON-YYYY
SELECT WR.EmpID AS EMPSID, SUM(WR.ESALARY) AS TOTAL_SALARY FROM WEEKLY_REPORT WR WHERE WR.EmpID = 'E009' AND TO_CHAR(WR.SDate,'MON-YYYY') = TO_CHAR('APR-2020','MON-YYYY') GROUP BY WR.EmpID;
错误 ORA-01722: invalid number
ORA-01722: invalid number
由于您的日期采用 dd-mm-yyyy 格式,因此您需要to_date(WR.SDate,'dd-mm-yyyy')在使用 to_char() 之前使用它。而不是 TO_CHAR('APR-2020','MON-YYYY')使用'APR-2020',这是一个有效的字符串。
dd-mm-yyyy
to_date(WR.SDate,'dd-mm-yyyy')
TO_CHAR('APR-2020','MON-YYYY')
'APR-2020'
SELECT WR.EmpID AS EMPSID, SUM(WR.ESALARY) AS TOTAL_SALARY FROM WEEKLY_REPORT WR WHERE WR.EmpID = 'E009' AND TO_CHAR(to_date(WR.SDate,'dd-mm-yyyy'),'MON-YYYY') = 'APR-2020' GROUP BY WR.EmpID;
数据库小提琴:
将日期转换为 mon-yyyy 的查询:
select TO_CHAR(to_date('10-04-2020','dd-mm-yyyy'),'MON-YYYY') result from dual
输出:
结果 2020 年 4 月 查询以检查条件:
select 'true' result from dual where TO_CHAR(to_date('10-04-2020','dd-mm-yyyy'),'MON-YYYY') = 'APR-2020'
结果
true