获取一周的开始和结束日期?在 Sql 服务器中? [英] Getting the Starting and ending date of week? In Sql server?

查看:38
本文介绍了获取一周的开始和结束日期?在 Sql 服务器中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在sql中,如何获取特定月份的一周的开始和结束日期.你能帮我吗.

In sql, how to get the starting and ending date of week for the particular month. Can you help me.

推荐答案

以下将适用于您认为一周的第一天(星期日、星期一等),只要确保您使用 SET DATEFIRST如果您想更改默认设置.SET DATEFIRST 1 将一周的第一天设为星期一.

The following will work whatever you consider the first day of the week (sunday, monday etc), just ensure you use SET DATEFIRST if you want to change from the default. SET DATEFIRST 1 will make the first day of the week monday.

SELECT  DATEADD(DAY, 1 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [WeekStart],
        DATEADD(DAY, 7 - DATEPART(WEEKDAY, GETDATE()), CAST(GETDATE() AS DATE)) [WeekEnd]

<小时>

编辑

我刚刚重新阅读了您的请求,我认为您可能追求与我上面给出的内容不同的内容.如果您想要一个月的第一天和最后一天的星期几,这将起作用:

I have just re-read you request and I think you may be after something different to what I have given above. If you want the day of the week of the first and last of the month this will do the trick:

SELECT  DATENAME(WEEKDAY, DATEADD(DAY, 1 - DATEPART(DAY, GETDATE()), GETDATE())) [FirstDayOfMonth],
        DATENAME(WEEKDAY, DATEADD(DAY, - DATEPART(DAY, DATEADD(MONTH, 1, GETDATE())), DATEADD(MONTH, 1, GETDATE()))) [LastDayOfMonth]

这篇关于获取一周的开始和结束日期?在 Sql 服务器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆