oracle 如何在 PLSQL 中修剪日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/391000/
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
How do I trim date in PLSQL?
提问by splattne
I have a date variable as 24-dec-08
.
I want only the 08
component from it.
我有一个日期变量作为24-dec-08
. 我只想要08
它的组件。
How do I do it in a select statement?
如何在 select 语句中执行此操作?
e.g.:
例如:
select db||sysdate
--(this is the component where I want only 08 from the date)
from gct;
回答by splattne
The easiest way is to use the to_char
function this way:
最简单的方法是这样使用to_char
函数:
to_char(sysdate, 'YY')
as documented here.
如此处所述。
If you need the integer value, you could use the extract
function for dates too. Take a look herefor a detailed description of the extract
syntax.
如果您需要整数值,您也可以将该extract
函数用于日期。查看此处了解extract
语法的详细说明。
For example:
例如:
extract(YEAR FROM DATE '2008-12-24')
would return 2008.
将返回2008。
If you just need the value of the last two digits, you could apply the modulo functionMOD
:
如果您只需要最后两位数字的值,您可以应用模函数MOD
:
mod(extract(YEAR FROM DATE '2008-12-24'), 100)
would return 8.
将返回8。
回答by Tony Andrews
The normal way to do this in Oracle is:
在 Oracle 中执行此操作的正常方法是:
select db||to_char(sysdate,'YY')
from gct;