简体   繁体   English

SQL转换周数到日期(年/月)

[英]SQL Convert Week Number to Date (dd/MM)

I am trying to convert the week number ( for example: 21 ) in SQL-Server to the date (from the Monday of that week) in dd/MM format. 我试图将SQL-Server中的周数( 例如: 21 )转换 dd / MM格式的日期(从该周的星期一开始)。

I have searched online but cannot seem to find anything that I could use. 我在网上搜索但似乎找不到任何我可以使用的东西。

Is this something I can do? 这是我能做的吗?

Any help or advice is appreciated. 任何帮助或建议表示赞赏。

Thank you in advance. 先感谢您。

Try this, 尝试这个,

declare @wk int  set @wk = 21
declare @yr int  set @yr = 2016

select dateadd (week, @wk-1, dateadd (year, @yr-1900, 0)) - 4 -
       datepart(dw, dateadd (week, @wk-1, dateadd (year, @yr-1900, 0)) - 4) + 1

or try this way 或尝试这种方式

declare @wk int  = 21

select dateadd(week,@wk-1, DATEADD(wk, DATEDIFF(wk,-1,DATEADD(yy, DATEDIFF(yy,0,getdate()), 0)), 0)) 

You can do it something like: 你可以这样做:

declare @Week_Number int, @Year int, @Year_Start_Day date, @Week_Day date

select 
    @Week_Number = 1,
    @Year = 2016

select @Year_Start_Day = cast(@Year as nvarchar(4)) + '0101'
select @Week_Day =  dateadd(wk, @Week_Number, @Year_Start_Day)

select dateadd(dd, 1 - datepart(weekday, @Week_Day), @Week_Day)

This will do: 这样做:

DECLARE @y int = 2016,
        @w int = 21

SELECT CONVERT(nvarchar(5),DATEADD(day,@w*7-(DATEPART(WEEKDAY,CAST(@y as nvarchar(4))+'-01-01')-2),CAST(@y as nvarchar(4))+'-01-01'),3)

Output: 输出:

23/05

How about this? 这个怎么样?

DECLARE @YearNum SMALLINT = 2016;
DECLARE  @WeekNum TINYINT=25;

select 
    SUBSTRING(CONVERT(VARCHAR(10),selected_date,105),0,6) AS WeeKDate
from 
(select DATEADD(dd,t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i,'1970-01-01') selected_date from
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
 (select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where YEAR(selected_date)=@YearNum
AND DATEPART(WK,selected_date)=@WeekNum
AND DATEPART(WEEKDAY,selected_date)=2 -- Monday

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

相关问题 在 SQL 中将日期 (yyyy-mm-dd) 转换为 MM-YYYY - Convert date (yyyy-mm-dd) to MM-YYYY in SQL 将日期MM / DD / YYYY转换为七位数 - Convert a date MM/DD/YYYY to a seven digit number pyspark sql 将日期格式从 mm/dd/yy hh:mm 或 yyyy-mm-dd hh:mm:ss 转换为 yyyy-mm-dd hh:mm 格式 - pyspark sql convert date format from mm/dd/yy hh:mm or yyyy-mm-dd hh:mm:ss into yyyy-mm-dd hh:mm format 雪花 SQL 将日期 'YYYY-MM-DD' 转换为 'DD-MM-YYYY' - Snowflake SQL convert date 'YYYY-MM-DD' to 'DD-MM-YYYY' ms sql将字符串转换为日期,将顺序从yyyy-mm-dd更改为dd.mm.yyyy - ms sql convert string to date changing the order from yyyy-mm-dd to dd.mm.yyyy SQL日期格式转换? [dd.mm.yy到YYYY-MM-DD] - SQL date format convert? [dd.mm.yy to YYYY-MM-DD] 如何在SQL Server中将“ DD / MM / YYYY”或“ YYYY-MM-DD”中的字符串转换为日期? - How to convert string in 'DD/MM/YYYY' or 'YYYY-MM-DD' into date in SQL Server? 将SQL列中的TEXT dd / mm / yyyy转换为DATE YYYY-MM-DD - convert TEXT dd/mm/yyyy in SQL column to DATE YYYY-MM-DD SQL将日期从dd.mm.yyyy转换为dd-mm-yyyy - SQL Convert Date from dd.mm.yyyy to dd-mm-yyyy 将 dd/mm/yy nvarchar(255) 转换为日期格式 dd/mm/yyyy 的 SQL 语句 - SQL statement to convert dd/mm/yy nvarchar(255) to Date format dd/mm/yyyy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM