使用 trunc(date) 的原因是什么

What is the reason to use trunc(date)

trunc有什么用?比如我想根据这个12-Mar-1996

显示人的年龄
select bo.journeystatus As "Journey Status",bo.bookingdate As "Booking Date",bo.fromdestination As "Location From",bo.todestination As "Desired Location", co.customerid As "Customer ID", co.customername As "Customer Name", co.customertel AS "Customer Phone No"
     , concat(TRUNC((SYSDATE - TO_DATE(co.customerdob, 'DD-MON-YYYY'))/ 365.25),' years old') as "Customer Age", co.customergender as "Gender"
from   booking bo, customer co
where  bo.journeystatus = 'Pending' and bo.bookingdate between to_date('01-'||in_month||'-'||in_year,'dd-mm-yyyy') AND to_date('30-'||in_month||'-'||in_year,'dd-mm-yyyy') and bo.customerid = co.customerid
group by bo.journeystatus, bo.fromdestination,bo.todestination, bo.bookingdate, co.customerid, co.customername, co.customertel, co.customerdob, co.customergender
order by bookingdate;

上面的说法是正确的,只是不明白为什么需要它?我去网上看了那些文章,还是没看懂?谁能用更简单的方式向我解释一下?

为什么需要这个 trunc(date)

concat(TRUNC((SYSDATE - TO_DATE(co.customerdob, 'DD-MON-YYYY'))/ 365.25),' years old')

让我们通过示例学习:

SELECT (SYSDATE - TO_DATE('30-03-1996', 'DD-MM-YYYY')) age
FROM DUAL;

-- Output - Total days (9281) and time (0.55410...) since 30-03-1996.
-- 
-- 9281.554108796296296296296296296296296296
--

SELECT (SYSDATE - TO_DATE('30-03-1996', 'DD-MM-YYYY')/ 365.25 age
FROM DUAL;

-- Output - Total years (25) and months/days/hours... (0.411519...) since 30-03-1996.
-- 
-- 25.41151953887494613025071615078459705428
--

-- Use trunc to cut off the decimal part
SELECT TRUNC((SYSDATE - TO_DATE('30-03-1996', 'DD-MM-YYYY'))/365.25) age
FROM DUAL;

-- Output - Total years (25) since 30-03-1996.
-- 
-- 25
--


啊!未格式化的 运行-on 代码真是一团糟。我必须通过自动格式化程序 运行 它才能阅读并理解它。这本身并不能回答你的问题,但我不能不加评论就让它通过,而且这个平台的限制不允许在 'comment'.

中进行解释
SELECT bo.journeystatus   AS "Journey Status",
       bo.bookingdate     AS "Booking Date",
       bo.fromdestination AS "Location From",
       bo.todestination   AS "Desired Location",
       co.customerid      AS "Customer ID",
       co.customername    AS "Customer Name",
       co.customertel     AS "Customer Phone No",
       Concat(Trunc(( SYSDATE - To_date(co.customerdob, 'DD-MON-YYYY') ) /
                    365.25),
       ' years old')      AS "Customer Age",
       co.customergender  AS "Gender"
FROM   booking bo,
       customer co
WHERE  bo.journeystatus = 'Pending'
       AND bo.bookingdate BETWEEN To_date('01-'
                                          ||in_month
                                          ||'-'
                                          ||in_year, 'dd-mm-yyyy') AND
                                      To_date('30-'
                                              ||in_month
                                              ||'-'
                                              ||in_year, 'dd-mm-yyyy')
       AND bo.customerid = co.customerid
GROUP  BY bo.journeystatus,
          bo.fromdestination,
          bo.todestination,
          bo.bookingdate,
          co.customerid,
          co.customername,
          co.customertel,
          co.customerdob,
          co.customergender
ORDER  BY bookingdate;  

这让我大吃一惊:

Concat(Trunc(( SYSDATE - To_date(co.customerdob, 'DD-MON-YYYY') ) /

函数TO_DATE接受一个字符串作为输入。这意味着两个条件之一为真:

  1. 列 co.customerdob 正如 to_date 所期望的那样,是字符串数据类型(VARCHAR 或 VARCHAR2)。如果是这种情况,则说明您存在严重的设计缺陷。 Dates/times 应该是两种可能的日期时间类型之一 - DATE 或 TIMESTAMP。存储为 DATE 或 TIMESTAMP 强制完整性并允许简单的 date/time 算术。存储为 VARCHAR 都不允许。

  1. 列 co.customerdob 是一个 DATE,因为它应该是。如果是这种情况,则您的代码有缺陷,因为将有一个隐式 TO_CHAR 函数,以便将 co.customerdob 放入 TO_DATE 期望的字符串中。而这个隐式 to_char 将取决于 NLS_DATE_FORMAT 的默认或会话设置,这可能是也可能不是您所期望和依赖的。