Oracle SQL:从日期中提取一年中的星期将得出随机结果 [英] Oracle SQL: Extracting the week of the year from date gives random results

查看:155
本文介绍了Oracle SQL:从日期中提取一年中的星期将得出随机结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用以下语句从日期中获取一年中的第几周:

I have been using the following statement to get the week of the year from a date:

TO_CHAR(TO_DATE(tbl.col,'YYYY/MM/DD'),'IW') AS week

现在,我收到的结果很奇怪,我似乎无法弄清楚.对于以下日期,我将收到第24周:

Now I'm receiving strange results and I can't seem to figure this out. For the following dates I receive the week 24:

17年6月19日|| 17年6月23日|| 2015年6月24日|| 2015年6月25日|| 17年6月29日|| 17年6月30日

19-Jun-17 || 23-Jun-17 || 24-Jun-17 || 25-Jun-17 || 29-Jun-17 || 30-Jun-17

为此,我收到25:

17年6月20日|| 17年6月21日|| 17年6月22日|| 17年6月26日|| 17年6月27日|| 17年6月28日

20-Jun-17 || 21-Jun-17 || 22-Jun-17 || 26-Jun-17 || 27-Jun-17 || 28-Jun-17

...然后继续.现在我所有的查询都显示不正确的数据,我有点担心.我直到现在才注意到它的唯一原因是因为第26、27、30、31和32周没有结果,而我的其他任何查询都不是这种情况.

...and this continues. I'm a bit worried now that all my queries show incorrect data. The only reason I have noticed it only now is because there are no results for weeks 26, 27, 30, 31 and 32 which is not the case in any of my other queries.

对任何可能出现错误的建议或指示,我们深表感谢.

Any suggestions or pointers towards possible errors are appreciated.

谢谢!

推荐答案

[TL; DR] 只需使用:TO_CHAR( tbl.col, 'IW' )

您有多个问题:

数据类型为日期"

The datatype is "date"

您正在使用DATE(不是VARCHAR2)调用TO_DATE( string, format_model ),这将导致Oracle使用NLS_DATE_FORMAT会话参数作为格式模型将DATE隐式转换为VARCHAR2只是为了您可以将其转换回DATE.因此,您正在有效地进行以下操作:

You are calling TO_DATE( string, format_model ) with a DATE (not a VARCHAR2) which causes Oracle to do an implicit conversion of the DATE to a VARCHAR2 using the NLS_DATE_FORMAT session parameter as the format model just so you can convert it back to a DATE. So, you are effectively doing:

TO_CHAR(
  TO_DATE(
    TO_CHAR(
      tbl.col,
      ( SELECT VALUE FROM NLS_SESSION_PARAMETERS WHERE PARAMETER = 'NLS_DATE_FORMAT' )
    ),
    'YYYY/MM/DD'
  ),
  'IW'
)

请勿执行此操作,只需使用:

TO_CHAR( tbl.col, 'IW' )

如果您这样做,那么以下问题将是无关紧要的.

If you do that then the following issues are irrelevant.

您正在使用YYYY作为年份格式模型,因此所有年份都将在公元1世纪.

You are using YYYY for the year format model so all the years are going to be in the 1st Century AD.

示例:

SELECT TO_CHAR( TO_DATE( '19-Jun-17', 'YYYY/MM/DD' ), 'YYYY-MM-DD' ) AS dt
FROM   DUAL

输出:

DT
----------
0019-06-17

如果您不打算解决隐式字符串转换的问题,那么您可能会想要:

If you aren't going to fix the implicit string conversion then you would probably want either:

TO_CHAR(TO_DATE(tbl.col,'DD-MON-YY'),'IW') AS week

或(取决于您希望99年是1999年还是2099年):

or (depending on whether you want the year 99 to be 1999 or 2099):

TO_CHAR(TO_DATE(tbl.col,'DD-MON-RR'),'IW') AS week

  • 您正在将MM格式模型用于MON格式化的数据-这不一定是问题,因为 MM也匹配MONMONTH ,但是您可能应该使用正确的模型.

  • You are using the MM format model for MON formatted data - this is not necessarily an issue as MM also matches MON and MONTH but you should probably use the correct model.

    这篇关于Oracle SQL:从日期中提取一年中的星期将得出随机结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆