SQL trunc(date, 'IW') 到底是什么?

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

What exactly does trunc(date, 'IW')?

sqloracleoracle11gdate-formatting

提问by Phate

For my project I need to have an absolute numerical correspondence between days of the week and 1...7 values.

对于我的项目,我需要在一周中的天数和 1...7 个值之间建立绝对的数字对应关系。

As you probably know the association between days and numbers can vary according to the locale, for example in Germany Monday is 1 and Sunday is 7, while in US Monday is 2 while Sunday is 1.

您可能知道天数和数字之间的关联会因地区而异,例如在德国,星期一是 1,星期日是 7,而在美国,星期一是 2,而星期日是 1。

So, searching for a solution, I found the following code which seems working regardless of the locale, assigning Monday=1...Sunday=7:

因此,在寻找解决方案时,我发现以下代码无论区域如何都可以正常工作,分配 Monday=1...Sunday=7:

1 + TRUNC (date) - TRUNC (date, 'IW')

Can someone explain me how does it work? In particular I just can't understand what this instruction:

有人可以解释一下它是如何工作的吗?特别是我无法理解这条指令是什么:

TRUNC (date, 'IW')

exactly does.

确实如此。

回答by John

TRUNC(DATE,'IW')returns the first day of the week. For me TRUNC(SYSDATE,'IW)returns Monday. Today is Tuesday Feb 21. Subtract from that TRUNC(SYSDATE,'IW')which would be Monday the 20th, and you'll get 1(because 21-20=1). Add 1onto that as you do in the beginning of your equation and you get 2, which we associate with Tuesday.

TRUNC(DATE,'IW')返回一周的第一天。对我来说TRUNC(SYSDATE,'IW)周一回来。今天是 2 月 21 日星期二。减去TRUNC(SYSDATE,'IW')20 日星期一,您将得到1(因为21-20=1)。1像你在方程开头所做的那样添加它,你会得到2,我们将其与星期二联系起来。

回答by Lalit Kumar B

The very basic concept of ISOweek is to make it NLS territory independent.

ISO周的最基本概念是使其NLS 区域独立

From documentation,

文档

Week of year (1-52 or 1-53) based on the ISO standard.

基于 ISO 标准的一年中的第几周(1-52 或 1-53)。

A week starts on a Mondayand ends on a Sunday.

一个星期开始在星期一和结束上周日