如何将星期数转换为日期?

How to convert number of week into date?(如何将星期数转换为日期?)
本文介绍了如何将星期数转换为日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一年和日历周,我如何获得该周的星期二作为日期?

解决方案

假设您有 yearcw(日历周)作为变量(例如来自 SELECT 语句)你可以得到如下日期:

DATE_SUB(DATE_ADD(MAKEDATE(year, 1), INTERVAL cw WEEK),间隔工作日(DATE_ADD(MAKEDATE(year, 1), INTERVAL cw WEEK)) -1天),

短语DATE_ADD(MAKEDATE(year, 1), INTERVAL cw WEEK)重复;不想存储变量.SQL 语句在 MySQL 上非常适合我.

更新: 只是为了澄清:WEEKDAY(DATE_ADD(MAKEDATE(year, 1), INTERVAL cw WEEK)) 将产生一周的第一天.从中减去一个数字(-1 代表星期二;-2 代表星期三,依此类推,将为您选择一周中的特定日期).请参阅此处.>

Given a year and calendar week, how can I get the tuesday of that week as a date?

解决方案

Given you have year and cw (calender week) as variables (e.g. from a SELECT statement) you can get the DATE as following:

DATE_SUB(
  DATE_ADD(MAKEDATE(year, 1), INTERVAL cw WEEK),
  INTERVAL WEEKDAY(
    DATE_ADD(MAKEDATE(year, 1), INTERVAL cw WEEK)
  ) -1 DAY),

The phrase DATE_ADD(MAKEDATE(year, 1), INTERVAL cw WEEK) is duplicated; did not want to store a variable. The SQL-Statement worked nicely for me on MySQL.

UPDATE: Just for clarification: WEEKDAY(DATE_ADD(MAKEDATE(year, 1), INTERVAL cw WEEK)) will yield the first day of the week. Substracting a number from it (-1 for Tuesday; -2 for Wednesday and so forth will select a specific day in the week for you). See here.

这篇关于如何将星期数转换为日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)