Oracle -获取星期几

g52tjvyc  于 2023-10-16  发布在  Oracle
关注(0)|答案(5)|浏览(94)

今天是星期二。
为什么当我运行这个SQL语句时,它说今天不是星期二?

SELECT 
CASE 
WHEN TO_CHAR(sysdate, 'Day') = 'Tuesday' THEN 'Its Tuesday'
ELSE 'Its Not Tuesday'  
END AS case_result,
TO_CHAR(sysdate, 'Day') AS day
FROM DUAL

回报率:

CASE_RESULT DAY
It's Not Tuesday    Tuesday
oxalkeyp

oxalkeyp1#

像这样,

SELECT CASE 
       WHEN trim(TO_CHAR(SYSDATE, 'Day')) = 'Tuesday' THEN 'Its Tuesday'
       ELSE 'Its Not Tuesday'  
       END AS case_result,
       TO_CHAR(SYSDATE, 'Day') AS DAY
FROM  DUAL;

或者按照ajmalmhd04的建议,你可以尝试使用FmDay,比如,

SELECT CASE 
       WHEN TO_CHAR(SYSDATE, 'FmDay') = 'Tuesday' THEN 'Its Tuesday'
       ELSE 'Its Not Tuesday'  
       END AS case_result,
       TO_CHAR(SYSDATE, 'Day') AS DAY
FROM  DUAL;
cx6n0qe3

cx6n0qe32#

to_char(sysdate,'day ')的值实际上是'Tuesday ',在Tuesday之后有两个空格。Oracle将该值填充为最长日的长度,即星期三,它是9个字符。
尝试

RTRIM(to_char(sysdate,'day'))
7qhs6swi

7qhs6swi3#

嗯,这个:

SELECT TO_CHAR(sysdate, 'Day') FROM dual;

在我的国家叫“沃托雷克”也许你是用你的语言,而不是用英语来获得本周的名字?
您可以使用以下命令检查数据库中设置的日期语言:

SELECT * FROM nls_database_parameters WHERE parameter = 'NLS_DATE_LANGUAGE';
rjjhvcjd

rjjhvcjd4#

SELECT 
CASE 
WHEN rtrim(TO_CHAR(sysdate, 'Day')) = 'Tuesday' THEN 'Its Tuesday'
ELSE 'Its Not Tuesday'  
END AS case_result,TO_CHAR(sysdate, 'Day') AS day
FROM DUAL
vpfxa7rd

vpfxa7rd5#

使用一周中更正式的数字位置将避免意外的填充和区域设置问题(正如@przemyslaw-kruglej所注意到的那样)。

SELECT
CASE
WHEN TO_CHAR(sysdate, 'D') = '2' THEN 'Its Tuesday'
ELSE 'Its Not Tuesday'
END AS case_result,
TO_CHAR(sysdate, 'Day') AS day
FROM DUAL

相关问题