oracle 如何在oracle中使用trunc date()函数。?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45932391/
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:27:18  来源:igfitidea点击:

how to use trunc date() function in oracle.?

oracleoracle11g

提问by Dipali Y.

When I run following query I got error.Can Someone help me to solve this.

当我运行以下查询时出现错误。有人可以帮我解决这个问题。

 select trunc('27-oct-97','dd-mm-yy') form dual;

when I run this query i got following error:

当我运行此查询时,出现以下错误:

Error:inline number function.

错误:内联数​​字函数。

回答by MT0

The TRUNCfunctionhas the signature:

TRUNC函数具有签名:

TRUNC( date_value, format )

You are providing a string value instead of a date value and 'dd-mm-yy'is an invalid format(you just want to truncate to the start of the day using 'dd'as the format or the start of the month using 'mm'or the start of the year using 'yy'- but using all three together does not make sense).

您提供的是字符串值而不是日期值并且'dd-mm-yy'无效格式(您只想截断到一天的开始使用'dd'作为格式或使用的月份'mm'的开始或使用的年份的开始'yy'- 但使用所有三个在一起没有意义)。

You can use ANSI date literals to specify the date:

您可以使用 ANSI 日期文字来指定日期:

SELECT TRUNC(
         DATE '1997-10-27',
         'DD'
       )
FROM   DUAL

Or use the TO_DATEfunction:

或者使用TO_DATE函数:

SELECT TRUNC(
         TO_DATE( '27-oct-97', 'DD-MON-RR' ),
         'DD'
       )
FROM   DUAL

(and you can also omit the 'DD'format argument as the default is to truncate to the start of the day.)

(你也可以省略'DD'格式参数,因为默认是截断到一天的开始。)

But, in both cases, the truncation is redundant as the date's time component is already midnight so does not need truncating.

但是,在这两种情况下,截断都是多余的,因为日期的时间部分已经是午夜,因此不需要截断。

What you want is a date value that has a non-midnight time component:

您想要的是具有非午夜时间组件的日期值:

SELECT TRUNC(
         TO_DATE( '1997-10-27 12:34:56', 'YYYY-MM-DD HH24:MI:SS' ),
         'DD'
       )
FROM   DUAL

or to truncate to the start of the month or year:

或截断到月份或年份的开头:

SELECT TRUNC(
         DATE '1997-10-27',
         'MM'
       )
FROM   DUAL

回答by sagi

You have two options:

您有两个选择:

1) Remove the time portion from the date truncate into the nearest date:

1)从日期截断到最近的日期中删除时间部分:

TRUNC(DATE)

2) Use format, to specify which values to trunc:

2) 使用格式,指定要截断的值:

TRUNC(DATE,year)

Truncates (will turn to 1) everything that is before the format , in this example the year. So this:

截断(将变为 1)格式之前的所有内容,在本例中为年份。所以这:

trunc(to_date('27-oct-97','dd-mm-yy'),'YEAR') 

Will output this:

将输出这个:

01-jan-97