在 oracle 中仅提取当前年份

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46884065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:28:15  来源:igfitidea点击:

Extract only the current year in oracle

oracle

提问by A.Wen

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.

我需要从 oracle 数据库中获取当前年份。例如,我需要将 2017 作为当前年份的答案作为数字类型返回。我尝试使用以下方式。

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

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

但它不起作用。那么最简单的方法是什么?

回答by Gurwinder Singh

You need to_charinstead of to_number

你需要to_char而不是 to_number

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

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

不过,这给出了一个字符串。您可以to_number进一步申请以转换为数字。

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

But there is better method using extract:

但是有更好的方法使用extract

select extract(year from sysdate) from student;

回答by a_horse_with_no_name

Use extract

extract

select extract(year from sysdate)
from dual;

回答by Abdul Razak

get current year start date: trunc (sysdate, 'yyyy')

获取当前年份的开始日期: trunc (sysdate, 'yyyy')

select trunc (sysdate, 'yyyy'),SYSDATE from dual;

get current Month start date:

获取当前月份的开始日期:

select trunc (sysdate, 'mm'),SYSDATE from dual; 

select records in a month/year:

选择一个月/一年的记录:

created_date between trunc (sysdate, 'mm') and sysdate;

created_date between trunc (sysdate, 'yyyy') and sysdate;