to_date 的转换表示缺少右括号

Conversion of to_date says missing right parenthesis

select A,B,C,
TO_DATE(year  ||'-'||  LPAD(month,2,0)  ||'-01','YYYY-MM-DD') as firstday,
LAST_DAY(TO_DATE(year  ||'-'||  LPAD(month,2,0)  ||'-01','YYYY-MM-DD')) as lastday
from test;

Error : "FROM keyword not found where expected"

一大堆东西。

SQL> with test (year, month) as
  2    (select '2022', '4' from dual)
  3  select to_date(year ||'-'|| lpad(month, 2, '0') || '-01', 'yyyy-mm-dd') result
  4  from test;

RESULT
--------------------
01-APR-22

SQL>

不过,您似乎想要那个月的 1 号,您可以将其缩短为

SQL> with test (year, month) as
  2    (select '2022', '4' from dual)
  3  select to_date(year || month, 'yyyymm') result
  4  from test;

RESULT
--------------------
01-APR-22

SQL>

LAST_DAY选项:

SQL> with test (year, month) as
  2    (select '2022', '4' from dual)
  3  select
  4             to_date(year  ||'-'||  lpad(month,2,0)  ||'-01','YYYY-MM-DD')  as firstday,
  5    last_day(to_date(year  ||'-'||  lpad(month,2,0)  ||'-01','YYYY-MM-DD')) as lastday
  6  from test;

FIRSTDAY   LASTDAY
---------- ----------
01.04.2022 30.04.2022

SQL>