SQLite ISO(即欧洲风格)周数
我想返回某个日期的 ISO 标准周数(即第 1-52/53 周)。 我尝试过使用内置函数 strftime,但没有成功。
任何人都可以建议一种无需编写自定义 C 或其他函数的方法。
I want to return ISO standard week numbers (ie week 1-52/53) from a date.
I have tried using the built in function strftime, but with no success.
Can anyone suggest a way without having to write a custom C or other function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道这是一个老问题,但最近我正在寻找同一问题的有效解决方案,这就是我想到的:
基本思想是通过简单地执行日期的整数除法来计算 ISO 周数查找日期 (my_date) 的星期四的年份乘以 7,得到介于 0 和 52 之间的结果,然后加 1。在除法之前减 1 只是为了对齐除法:例如,第一周的星期四,一年中的某一天可以在 1 到 7 之间,我们希望除法的结果为 0所有情况,因此我们需要从一年中的某一天减去 1,然后再除以 7。
I know this is an old question, but recently I was looking for an efficient solution for the same problem, and this is what I came up with:
The basic idea is to calculate the ISO week number by simply performing integer division of the day of year of the Thursday of the date being looked up (my_date) by 7, giving a result between 0 and 52, and then adding 1 to it. And the subtraction by 1 just before the division is there just for the alignment of the division: Thursday of week 1, for example, can have a day of year between 1 and 7, and we want the result of the division to be 0 in all cases, so we need to subtract 1 from the day of year before dividing it by 7.
您具体尝试了什么?
这对我有用(使用 Python REPL 中的内置 SQLite):
What specifically did you try?
This works for me (using built-in SQLite from Python REPL):
好的,所以我设法回答我自己的问题。对于任何可能需要类似解决方案的人,这就是我想出的。请注意,我没有 IT 背景,我的 SQL 是自学的。
一般过程
翻译成 SQLite...
如果有人可以改进这个 SQL,我将不胜感激。
OK, so I managed to answer my own question. For anyone who might need a similar solution, this is what I came up with. Please note, I do not have an IT background and my SQL is self taught.
General process
Translated into SQLite....
If anyone can improve this SQL, I would appreciate the feedback.