sql中的每周明智日期

week wise date in sql
2021-07-22
  •  译文(汉语)
  •  原文(英语)

我正在C#.net中开发一个应用程序,用户将在其中选择年份,并希望向他显示每周的日期.

例如2013年

Week  Start Date    End Date
------------------------------
1     2013-01-01    2013-01-06

持续到2013年12月31日

我已经创建了一个存储过程,通过它可以得到上面的输出.

但第一周应从2013年12月31日星期一开始.

请帮忙.

问候,

祖伯·卡兹(Zuber Kazi)

速聊1:
为什么不使用DateTimePicker?
速聊2:
请检查星期一的星期几与否.
速聊3:
您可以发布用于生成此输出的存储过程吗?
速聊4:
为什么要在SQL中执行此操作?
解决过程1

使用以下逻辑来确定第一周的第一天

DECLARE @Date DATETIME=CONVERT(DATETIME,'01-01-'+ CONVERT(NVARCHAR(4), YEAR(GETDATE())) )
SELECT DATEADD(d,(DATEPART(dw,@Date)-2)*-1,@Date )

这将帮助您确定每个工作日的索引.索引从1开始,从星期日开始

SELECT (DATEPART(dw,@Date))
速聊1:
尊敬的巴拉特·坦克斯(Bharat Thanx):它的工作时间是周一至周六,但对于周日,则需要从第二天开始.
速聊2:
我认为可以设置一周的第一天
解决过程2

为什么要在SQL中执行此操作?采取一种方法并保持这种方法在page_load

            DateTime da = DateTime.Now;
            int offset = da.DayOfWeek - DayOfWeek.Monday;
            DateTime lastMonday = da.AddDays(-offset);
            Label1.Text = lastMonday.ToShortDateString();
            Label2.Text = lastMonday.AddDays(+1).ToShortDateString();
            Label3.Text = lastMonday.AddDays(+2).ToShortDateString();
            Label4.Text = lastMonday.AddDays(+3).ToShortDateString();
            Label5.Text = lastMonday.AddDays(+4).ToShortDateString();
            Label6.Text = lastMonday.AddDays(+5).ToShortDateString();
            Label7.Text = lastMonday.AddDays(+6).ToShortDateString();

I am developing an application in C# .net in which user will select a year and I want to show him week wise dates.

e.g. for year 2013

Week  Start Date    End Date
------------------------------
1     2013-01-01    2013-01-06

it goes on till 31st Dec 2013

I have already created a stored procedure using which I get above output.

But for the first week it should start from Monday which is 2013-12-31.

Please help.

Regards,

Zuber Kazi

Talk1:
Why not use the DateTimePicker ?
Talk2:
Please, check with day of week in monday or not.
Talk3:
can you post the stored proc you used to generate this output?
Talk4:
Why are you doing this in SQL?
Solutions1

use following logic to decide first day of first week

DECLARE @Date DATETIME=CONVERT(DATETIME,'01-01-'+ CONVERT(NVARCHAR(4), YEAR(GETDATE())) )
SELECT DATEADD(d,(DATEPART(dw,@Date)-2)*-1,@Date )

This will help you to decide which index is for any week day. Index starts from 1 and from Sunday

SELECT (DATEPART(dw,@Date))
Talk1:
Dear Bharat Thanx for the help. It is working from Monday to Saturday but for Sunday it takes next day as start.
Talk2:
I think this is possible to set the first day of the week
Solutions2

Why are you doing this in SQL? Take a method and keep this method in page_load

            DateTime da = DateTime.Now;
            int offset = da.DayOfWeek - DayOfWeek.Monday;
            DateTime lastMonday = da.AddDays(-offset);
            Label1.Text = lastMonday.ToShortDateString();
            Label2.Text = lastMonday.AddDays(+1).ToShortDateString();
            Label3.Text = lastMonday.AddDays(+2).ToShortDateString();
            Label4.Text = lastMonday.AddDays(+3).ToShortDateString();
            Label5.Text = lastMonday.AddDays(+4).ToShortDateString();
            Label6.Text = lastMonday.AddDays(+5).ToShortDateString();
            Label7.Text = lastMonday.AddDays(+6).ToShortDateString();
转载于:https://stackoverflow.com/questions/17377695/week-wise-date-in-sql

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入