如何从日期中获取星期几名称?

发布于 2024-12-13 12:07:40 字数 94 浏览 0 评论 0原文

给定 03/09/1982 我们怎么能说这是星期几呢?在本例中,时间为星期二

是否可以在单个查询中获取?

Given 03/09/1982 how can we say it is which week day. In this case it will be Tue.

Is it possible to get in a single query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

回忆那么伤 2024-12-20 12:07:40
SQL> SELECT TO_CHAR(date '1982-03-09', 'DAY') day FROM dual;

DAY
---------
TUESDAY

SQL> SELECT TO_CHAR(date '1982-03-09', 'DY') day FROM dual;

DAY
---
TUE

SQL> SELECT TO_CHAR(date '1982-03-09', 'Dy') day FROM dual;

DAY
---
Tue

(请注意,查询使用 ANSI 日期文字,它遵循 ISO-8601 日期标准并避免日期格式歧义。)

SQL> SELECT TO_CHAR(date '1982-03-09', 'DAY') day FROM dual;

DAY
---------
TUESDAY

SQL> SELECT TO_CHAR(date '1982-03-09', 'DY') day FROM dual;

DAY
---
TUE

SQL> SELECT TO_CHAR(date '1982-03-09', 'Dy') day FROM dual;

DAY
---
Tue

(Note that the queries use ANSI date literals, which follow the ISO-8601 date standard and avoid date format ambiguity.)

如歌彻婉言 2024-12-20 12:07:40

晚了将近十年,但我认为有必要增强@Zohaib 的答案,因为它的结果取决于客户的语言。如果您向用户显示星期几名称,那绝对没问题,但如果您的代码依赖于它,我宁愿像这样控制语言:

SQL> SELECT TO_CHAR(date '1982-03-09', 'DAY', 'NLS_DATE_LANGUAGE = ENGLISH') day FROM dual;

DAY
---------
TUESDAY

SQL> SELECT TO_CHAR(date '1982-03-09', 'DY', 'NLS_DATE_LANGUAGE = ENGLISH') day FROM dual;

DAY
---
TUE

SQL> SELECT TO_CHAR(date '1982-03-09', 'Dy', 'NLS_DATE_LANGUAGE = ENGLISH') day FROM dual;

DAY
---
Tue

Nearly ten years late to the party, but I think it's neccessary to enhance @Zohaib 's answer as it's result depend on the language of the client. If you display the week day name to a user, it's absolutely fine, but if your code depends on it, I'd rather control the language like so:

SQL> SELECT TO_CHAR(date '1982-03-09', 'DAY', 'NLS_DATE_LANGUAGE = ENGLISH') day FROM dual;

DAY
---------
TUESDAY

SQL> SELECT TO_CHAR(date '1982-03-09', 'DY', 'NLS_DATE_LANGUAGE = ENGLISH') day FROM dual;

DAY
---
TUE

SQL> SELECT TO_CHAR(date '1982-03-09', 'Dy', 'NLS_DATE_LANGUAGE = ENGLISH') day FROM dual;

DAY
---
Tue
入怼 2024-12-20 12:07:40

为了在 Oracle sql 中执行此操作,我尝试了这样的操作,它对我有用

SELECT 
    START_DATE,
    CASE WHEN START_DAY = 7 THEN 'SUNDAY'
         WHEN START_DAY = 1 THEN 'MONDAY'
         WHEN START_DAY = 2 THEN 'TUESDAY'
         WHEN START_DAY = 3 THEN 'WEDNESDAY'
         WHEN START_DAY = 4 THEN 'THURSDAY'
         WHEN START_DAY = 5 THEN 'FRIDAY'
         WHEN START_DAY = 6 THEN 'SATURDAY'
    END DAY_NAME
FROM 
(SELECT 
    TO_CHAR(T.START_DATE, 'DY') START_DAY,
    TO_CHAR(T.START_DATE, 'MM/DD/YYYY') START_DATE
       
FROM 
    TABLE T )

To do this in Oracle sql, I tried like this and it worked for me

SELECT 
    START_DATE,
    CASE WHEN START_DAY = 7 THEN 'SUNDAY'
         WHEN START_DAY = 1 THEN 'MONDAY'
         WHEN START_DAY = 2 THEN 'TUESDAY'
         WHEN START_DAY = 3 THEN 'WEDNESDAY'
         WHEN START_DAY = 4 THEN 'THURSDAY'
         WHEN START_DAY = 5 THEN 'FRIDAY'
         WHEN START_DAY = 6 THEN 'SATURDAY'
    END DAY_NAME
FROM 
(SELECT 
    TO_CHAR(T.START_DATE, 'DY') START_DAY,
    TO_CHAR(T.START_DATE, 'MM/DD/YYYY') START_DATE
       
FROM 
    TABLE T )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文