简体   繁体   English

Oracle SQL:从日期中提取一年中的星期给出随机结果

[英]Oracle SQL: Extracting the week of the year from date gives random results

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

Now I'm receiving strange results and I can't seem to figure this out. 现在我收到奇怪的结果,我似乎无法弄清楚。 For the following dates I receive the week 24: 对于以下日期,我将收到第24周的信息:

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

For these, I receive 25: 为此,我收到25:

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

...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. 我现在才注意到的唯一原因是因为第26、27、30、31和32周没有结果,而我的其他任何查询都不是这种情况。

Any suggestions or pointers towards possible errors are appreciated. 任何建议或指向可能的错误的指针,不胜感激。

Thank you! 谢谢!

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

You have multiple issues: 您有多个问题:

  1. The datatype is "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 . 您正在调用带有DATE (不是VARCHAR2 )的TO_DATE( string, format_model ) ,这会导致Oracle使用NLS_DATE_FORMAT会话参数作为格式模型将DATE隐式转换为VARCHAR2 ,从而可以将其转换回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' ) 

    Do not do this , just use: 不要这样做 ,只需使用:

     TO_CHAR( tbl.col, 'IW' ) 

    If you do that then the following issues are irrelevant. 如果这样做,则以下问题无关紧要。

  2. As RealCheeseLord points out, the position of year and day in your format model is reversed; 正如RealCheeseLord所指出的,格式模型中年和日的位置是相反的。 and
  3. You are using YYYY for the year format model so all the years are going to be in the 1st Century AD. 您正在使用YYYY作为年格式模型,因此所有年份都将在公元1世纪。

    Example : 范例

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

    Output : 输出

     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 

    or (depending on whether you want the year 99 to be 1999 or 2099): 或(取决于您希望99年是1999年还是2099年):

     TO_CHAR(TO_DATE(tbl.col,'DD-MON-RR'),'IW') AS week 
  4. 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. 您正在将MM格式模型用于MON格式的数据-这不一定是问题,因为MM也与MONMONTH匹配,但是您可能应该使用正确的模型。

You have the wrong order of year and day. 您的年月日顺序错误。 those weeks are correct for example 17.06.2020 is week 25, 17.06.2023 is week 24 and so on. 那些星期是正确的,例如,2020年6月17日是第25周,2023年6月17日是第24周,依此类推。

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

or something 或者其他的东西

examples at bottom of page 页面底部的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM