gpt4 book ai didi

sql - 如何在sql中提取周数

转载 作者:行者123 更新时间:2023-12-03 13:07:18 25 4
gpt4 key购买 nike

我有一个 varchar2 类型的 transdate 列,它具有以下主菜

01/02/2012
01/03/2012

等等。

我使用 to_date 函数将其转换为另一列中的日期格式。这是我得到的格式。
01-JAN-2012
03-APR-2012

当我试图提取weekno时,我得到了所有空值。

从表名中选择 to_char(to_date(TRANSDATE), 'w') 作为 weekno。
null
null

如何以上述格式从日期获取weekno?

最佳答案

转换您的 varchar2 后日期为真 date数据类型,然后转换回 varchar2使用所需的掩码:

to_char(to_date('01/02/2012','MM/DD/YYYY'),'WW')

如果您想要 number 中的周数数据类型,您可以将语句包装在 to_number() 中:
to_number(to_char(to_date('01/02/2012','MM/DD/YYYY'),'WW'))

但是,您有几个周数选项需要考虑:
WW  Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.
W Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.
IW Week of year (1-52 or 1-53) based on the ISO standard.

关于sql - 如何在sql中提取周数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16530044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com