【发布时间】:2021-10-27 07:51:01
【问题描述】:
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')
【问题讨论】:
-
在 Oracle trunc(date) 中删除时间元素。仅返回日期(午夜)。数据类型 DATE 始终存储日期 + 时间。
-
我明白了,但是,如果 sysdate - todate(),值如何返回 (25),我是否指定只扣除年份?
-
"TRUNC 删除数字的小数部分。INT 根据数字小数部分的值将数字向下舍入到最接近的整数。" Round vs. Trunc。只需删除您的 TRUNC() 函数,您就可以看到不同之处:)
-
请注意,您的示例不是
trunc(date),而是trunc(number),因为sysdate - someotherdate给出了天数的差异。此外,co.customerdob需要转换为日期似乎很奇怪。通常,您会将其存储为日期,并且不需要任何转换。