TRUNC 解释

guolin_cai 2009-09-10 11:54:32
SQL> select trunc(sysdate,'w') a from dual;

A
-----------
9/8/2009


SQL> select trunc(sysdate,'year') from dual;

TRUNC(SYSDATE,'YEAR')
---------------------
1/1/2009


这里的 TRUNC(SYSDATE,'W') 为什么是 8号?按我的理解应该是本周的第一天

SQL> select trunc(sysdate,'ww') from dual;

TRUNC(SYSDATE,'WW')
-------------------
9/10/2009


还有这个,没理解通
...全文
736 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
doer_ljy 2009-09-10
  • 打赏
  • 举报
回复
Format Model Rounding or Truncating Unit
CC
SCC
One greater than the first two digits of a four-digit year

SYYYY
YYYY
YEAR
SYEAR
YYY
YY
Y
Year (rounds up on July 1)

IYYY
IY
IY
I
ISO Year

Q
Quarter (rounds up on the sixteenth day of the second month of the quarter)

MONTH
MON
MM
RM
Month (rounds up on the sixteenth day)

WW
Same day of the week as the first day of the year

IW
Same day of the week as the first day of the ISO year

W
Same day of the week as the first day of the month

DDD
DD
J
Day

DAY
DY
D
Starting day of the week

HH
HH12
HH24
Hour

MI
Minute
qin_phoenix 2009-09-10
  • 打赏
  • 举报
回复
SQL> --截取今天:
SQL> select sysdate,trunc(sysdate,'dd') from dual;

SYSDATE TRUNC(SYSDATE,'DD')
----------- -------------------
2009-09-10 2009-09-10
SQL> --截取本周第一天:
SQL> select sysdate,trunc(sysdate,'d') from dual;

SYSDATE TRUNC(SYSDATE,'D')
----------- ------------------
2009-09-10 2009-09-06
SQL> --截取本月第一天:
SQL> select sysdate,trunc(sysdate,'mm') from dual;

SYSDATE TRUNC(SYSDATE,'MM')
----------- -------------------
2009-09-10 2009-09-01
SQL> --截取本年第一天:
SQL> select sysdate,trunc(sysdate,'y') from dual;

SYSDATE TRUNC(SYSDATE,'Y')
----------- ------------------
2009-09-10 2009-01-01
SQL> --截取到小时:
SQL> select sysdate,trunc(sysdate,'hh') from dual;

SYSDATE TRUNC(SYSDATE,'HH')
----------- -------------------
2009-09-10 2009-09-10 12:00:00
SQL> --截取到分钟:
SQL> select sysdate,trunc(sysdate,'mi') from dual;

SYSDATE TRUNC(SYSDATE,'MI')
----------- -------------------
2009-09-10 2009-09-10 12:02:00

SQL>
guolin_cai 2009-09-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 guolin_cai 的回复:]
引用 4 楼 doer_ljy 的回复:
引用 3 楼 guolin_cai 的回复:
w /ww 这两个是什么意思?


没看到吗?
WW
Same day of the week as the first day of the year

W
Same day of the week as the first day of the month


英语我是看的一知半解的。。不过感觉这个解释只适合 TO_CHAR不适合TRUNC


[/Quote]

给分给错了,,本来是要给你分的。不好意思
guolin_cai 2009-09-10
  • 打赏
  • 举报
回复
可以了,
guolin_cai 2009-09-10
  • 打赏
  • 举报
回复
我现在要的不是转换的结果。。而是想知道为什么TRUNC后会是这个结果
ojuju10 2009-09-10
  • 打赏
  • 举报
回复
还是用to_char转换吧

trunc老是莫名奇妙地截取部分数据
inthirties 2009-09-10
  • 打赏
  • 举报
回复
WW
Same day of the week as the first day of the year
和该年第一天所在的星期里一样的天。
比如2009-01-01是星期四
所以这里返回的是本周四对于的时间,所以返回的是10号
SQL> select trunc(sysdate,'WW') a from dual;
A
--------------
10-9月 -09

W
Same day of the week as the first day of the month
和本月的第一天在星期里一样的天数
SQL> select trunc(sysdate,'W') a from dual;
本月是2009-09-01是星期二
所以返回的是本周的星期二
也就是
A
--------------
08-9月 -09
guolin_cai 2009-09-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 doer_ljy 的回复:]
引用 3 楼 guolin_cai 的回复:
w /ww 这两个是什么意思?


没看到吗?
WW
Same day of the week as the first day of the year

W
Same day of the week as the first day of the month
[/Quote]

英语我是看的一知半解的。。不过感觉这个解释只适合 TO_CHAR不适合TRUNC

doer_ljy 2009-09-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 guolin_cai 的回复:]
w /ww 这两个是什么意思?

[/Quote]
没看到吗?
WW
Same day of the week as the first day of the year

W
Same day of the week as the first day of the month
guolin_cai 2009-09-10
  • 打赏
  • 举报
回复
w /ww 这两个是什么意思?

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧