豁免星期日的Oracle SQL脚本

huangapple go评论104阅读模式
英文:

Exempting sunday from oracle sql script

问题

请帮我检查下面的脚本

SELECT CASE
       WHEN to_char(sysdate - 1, 'DAY') = 'SUNDAY' THEN
          trunc(sysdate - 2)
       ELSE
          trunc(sysdate - 1)
       END AS day
  FROM dual

我希望如果 CASE 中的条件为真,则返回 2 天,否则返回前一天,但脚本一直返回前一天。

英文:

Kindly help me check the script below

SELECT CASE
       WHEN to_char(sysdate - 1, 'DAY') = 'SUNDAY' THEN
          trunc(sysdate - 2)
       ELSE
          trunc(sysdate - 1)
       END AS day
  FROM dual

I wish to return 2 days if the condition in the case is true else to return previous day but the script keeps returning the previous day

答案1

得分: 3

要删除所有与语言相关的内容,您可以使用以下代码来计算自 ISO 周的开始以来的天数(ISO 周始终从星期一开始):

SELECT CASE TRUNC( SYSDATE - 1 ) - TRUNC( SYSDATE - 1, 'IW' )
       WHEN 6                                                 -- 星期天
       THEN TRUNC( SYSDATE - 2 )
       ELSE TRUNC( SYSDATE - 1 )
       END AS day 
FROM   DUAL

db<>fiddle

英文:

To remove all language-dependencies you can use TRUNC(SYSDATE - 1) - TRUNC(SYSDATE - 1, &#39;IW&#39;) to count the number of days since the start of the iso-week (which is always on a Monday):

SELECT CASE TRUNC( SYSDATE - 1 ) - TRUNC( SYSDATE - 1, &#39;IW&#39; )
       WHEN 6                                                 -- Sunday
       THEN TRUNC( SYSDATE - 2 )
       ELSE TRUNC( SYSDATE - 1 )
       END AS day 
FROM   DUAL

db<>fiddle

答案2

得分: 2

当您使用 TO_CHAR(sysdate-1,'DAY') 时,结果会填充空格。尝试使用 SELECT '&quot;'||TO_CHAR(sysdate-1,'DAY')||'&quot;' 以查看效果。

使用 格式模型修饰符 FM,您还应该明确定义语言:

TO_CHAR(sysdate-1, 'fmDAY', 'NLS_DATE_LANGUAGE = American') = 'SUNDAY'

或者

TRIM(TO_CHAR(sysdate-1, 'DAY', 'NLS_DATE_LANGUAGE = American')) = 'SUNDAY'

英文:

When you use TO_CHAR(sysdate-1,&#39;DAY&#39;) then result is padded with spaces. Try SELECT &#39;&quot;&#39;||TO_CHAR(sysdate-1,&#39;DAY&#39;)||&#39;&quot;&#39; in order to see the effect.

Use Format Model Modifiers FM and you should define also the language explicitly:

TO_CHAR(sysdate-1, &#39;fmDAY&#39;, &#39;NLS_DATE_LANGUAGE = American&#39;) = &#39;SUNDAY&#39;

or

TRIM(TO_CHAR(sysdate-1, &#39;DAY&#39;, &#39;NLS_DATE_LANGUAGE = American&#39;)) = &#39;SUNDAY&#39;

答案3

得分: 2

使用 DY -- 它始终是3个字符:

SELECT (CASE WHEN to_char(sysdate - 1, 'DY') = 'SUN' 
             THEN trunc(sysdate - 2)
             ELSE trunc(sysdate - 1)
        END) AS day
FROM dual;

为了正式正确,您应该在第三个参数中包含一个NLS参数('NLS_DATE_LANGUAGE = American'),以确保结果是英文的。

英文:

Use DY -- it is always 3 characters:

SELECT (CASE WHEN to_char(sysdate - 1, &#39;DY&#39;) = &#39;SUN&#39; 
             THEN trunc(sysdate - 2)
             ELSE trunc(sysdate - 1)
        END) AS day
FROM dual;

To be formally correct, you should include an NLS parameter as the third argument (&#39;NLS_DATE_LANGUAGE = American&#39;) to be sure that the results are in English.

huangapple
  • 本文由 发表于 2020年1月6日 18:00:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定