Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract only the current year in oracle

I need to get the current year from the oracle db. For an example I need to return 2017 as the answer for the current year as a number type. I tried using following way.

select to_Number(sysdate, 'YYYY') from student s

But it not works. So what is the easiest way?

like image 463
A.Wen Avatar asked Dec 07 '25 06:12

A.Wen


2 Answers

You need to_char instead of to_number

select to_char(sysdate, 'YYYY') from student;

That give a string though. you could apply to_number on it further to convert into number.

select to_number(to_char(sysdate, 'YYYY')) from student;

But there is better method using extract:

select extract(year from sysdate) from student;
like image 165
Gurwinder Singh Avatar answered Dec 10 '25 03:12

Gurwinder Singh


Use extract

select extract(year from sysdate)
from dual;

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!