英文:
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
英文:
To remove all language-dependencies you can use TRUNC(SYSDATE - 1) - TRUNC(SYSDATE - 1, 'IW')
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, 'IW' )
WHEN 6 -- Sunday
THEN TRUNC( SYSDATE - 2 )
ELSE TRUNC( SYSDATE - 1 )
END AS day
FROM DUAL
答案2
得分: 2
当您使用 TO_CHAR(sysdate-1,'DAY')
时,结果会填充空格。尝试使用 SELECT '"'||TO_CHAR(sysdate-1,'DAY')||'"'
以查看效果。
使用 格式模型修饰符 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,'DAY')
then result is padded with spaces. Try SELECT '"'||TO_CHAR(sysdate-1,'DAY')||'"'
in order to see the effect.
Use Format Model Modifiers FM
and you should define also the language explicitly:
TO_CHAR(sysdate-1, 'fmDAY', 'NLS_DATE_LANGUAGE = American') = 'SUNDAY'
or
TRIM(TO_CHAR(sysdate-1, 'DAY', 'NLS_DATE_LANGUAGE = American')) = 'SUNDAY'
答案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, 'DY') = 'SUN'
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 ('NLS_DATE_LANGUAGE = American'
) to be sure that the results are in English.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论