Reputation: 223
I need a sql query to get a 1st day of the 1st month of the year which i am passing.
ex: If i am passing 2021, my output should be 01-JAN-2021 If i am passing 2020, my output should be 01-JAN-2020
Upvotes: 2
Views: 1683
Reputation: 3439
If you pass the year as a number, you can get the desired result with the following code:
with yy(yy) as (
select 2021 from dual union all
select 2020 from dual union all
select 1970 from dual
)
select
add_months(date'0000-01-01', yy * 12) y1,
date'0000-01-01' + TO_YMINTERVAL(to_char(yy)||'-0') y2
from yy;
Result:
+------------+------------+
| Y1 | Y2 |
+------------+------------+
| 2021-01-01 | 2021-01-01 |
| 2020-01-01 | 2020-01-01 |
| 1970-01-01 | 1970-01-01 |
+------------+------------+
Upvotes: 1
Reputation: 168232
Assuming you are passing the year as the bind variable :year
then:
SELECT TO_DATE(:year||'0101','YYYYMMDD')
FROM DUAL;
or
SELECT TRUNC(TO_DATE(:year,'YYYY'),'YY')
FROM DUAL;
Upvotes: 1