南大通用GBase 8a获取指定日期是所在周第几天的dayofweek 和 weekday函数介绍和例子

本文介绍在GBase 8a数据库中,内置的获取指定日期是所在周第几天的dayofweek 和 weekday函数介绍和例子

WEEKDAY

语法

WEEKDAY(date)

说明

返回 date 对应的星期索引(0=Monday,1=Tuesday,…6=Sunday)。

星期一是第一天,返回0。

用例

2021-01-01是星期五,对应本函数的返回值4。

gbase> select weekday('2021-01-01');
+-----------------------+
| weekday('2021-01-01') |
+-----------------------+
|                     4 |
+-----------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select now(),weekday(now());
+---------------------+----------------+
| now()               | weekday(now()) |
+---------------------+----------------+
| 2021-01-08 09:22:06 |              4 |
+---------------------+----------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select current_date,weekday(current_date);
+--------------+-----------------------+
| current_date | weekday(current_date) |
+--------------+-----------------------+
| 2021-01-08   |                     4 |
+--------------+-----------------------+
1 row in set (Elapsed: 00:00:00.00)

DAYOFWEEK

语法

DAYOFWEEK(date)

说明

返回 date (1 = 周日, 2 = 周一, …, 7 = 周六)对应的工作日索引。

星期日是第一天,返回1。

用例

gbase> select dayofweek('2021-01-01');
+-------------------------+
| dayofweek('2021-01-01') |
+-------------------------+
|                       6 |
+-------------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select now(),dayofweek(now());
+---------------------+------------------+
| now()               | dayofweek(now()) |
+---------------------+------------------+
| 2021-01-08 09:28:58 |                6 |
+---------------------+------------------+
1 row in set (Elapsed: 00:00:00.00)

gbase> select current_date,dayofweek(current_date);
+--------------+-------------------------+
| current_date | dayofweek(current_date) |
+--------------+-------------------------+
| 2021-01-08   |                       6 |
+--------------+-------------------------+
1 row in set (Elapsed: 00:00:00.00)

总结

2个函数都是返回所在周的第几天,但

1、weekday是从周一开始计算;dayofweek是从周日开始。
2、weekday返回值从0开始;dayofweek返回值从1开始。

所以,相同的周中间日期,比如周五,两个函数返回值差2。