简体   繁体   English

SQL:星期一的第一天

[英]SQL: First day of week Monday

I would like to display Monday as the first day of the week and Sunday the last day of the week.我想将星期一显示为一周的第一天,将星期日显示为一周的最后一天。 I am using for this report the Report server.我正在为此报告使用报告服务器。

my query:我的查询:

   Select
Datum,
Sum(Prod) as Prod

FROM ( 
Select 

intervaldate as Datum,
Sum(case when TabName = 'Produzierte Dosen' then DisplayUnits else 0 end) as Prod


from vwOeeIntervalCount 
where 
IntervalDateWeek >=  dateadd(wk, datediff(wk, 0, getdate()) - 1, 0)  
and IntervalDateWeek < dateadd(wk, datediff(wk, 0, getdate()), 0)
and  IntervalDate >= dateadd(day,datediff(day,0,GETDATE())-6,0)
AND IntervalDate < dateadd(day,datediff(day,0,GETDATE()),0) 

and CalculationName = 'Packaging'

group by intervaldate
)c

group by Datum

Here the result:结果如下:

Date                      |Prod 
2018-02-25 00:00:00.000   |1836528
2018-02-26 00:00:00.000   |8131127,99999999

EDIT: Sorry here is my question.编辑:对不起,这是我的问题。 I would like to display the Monday as the first day of the week.我想将星期一显示为一周的第一天。 My query displays the Sunday as the first day of the week.我的查询将星期日显示为一周的第一天。 How can I do that?我怎样才能做到这一点?

By default MS SQL Server has configured sunday as first day of a week.默认情况下,MS SQL Server 已将星期日配置为一周的第一天。

You can set the server config for the first day of a week to any day with the following command:您可以使用以下命令将一周第一天的服务器配置设置为任何一天:

SET DATEFIRST { number | @number_var } 

Value   First day of the week is
1   Monday
2   Tuesday
3   Wednesday
4   Thursday
5   Friday
6   Saturday
7 (default, U.S. English)   Sunday

Source: https://docs.microsoft.com/en-us/sql/t-sql/statements/set-datefirst-transact-sql来源: https : //docs.microsoft.com/en-us/sql/t-sql/statements/set-datefirst-transact-sql

you can use this to the current week:您可以将其用于本周:

/*-- Week Start
--1->Sunday --2->Monday....*/
SELECT CAST(DATEADD(dd, -(DATEPART(dw,  GETDATE()))+2, GETDATE()) AS DATE) [WeekStart], 
       CAST(DATEADD(dd, 8-(DATEPART(dw, GETDATE())), GETDATE()) AS DATE) [WeekEnd]
  /*-- Week End
  --7->Saturday --8-> Sunday...*/

Running this today will produce this result:今天运行这个将产生这样的结果:

WeekStart  WeekEnd
---------- ----------
2018-02-26 2018-03-04

if you have dates in your table, you can use them instead of the GETDATE()如果您的表中有日期,则可以使用它们代替GETDATE()

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

相关问题 设置星期一是一周的第一天 - set Monday is first day of week 使用存储过程将星期一计算为一周的第一天 - Calculate Monday as first day of the week with a stored procedure 在Oracle中,“每周的第一天”设置为星期日,而不是星期一 - First Day of Week is set to Sunday instead of Monday in Oracle 如何在视图中使datepart()函数接受星期一作为一周的第一天 - How to make a datepart() function in a view accept Monday as the first day of the week 前一天的一周开始(星期一) - week beginning (Monday) for previous day 假设每月的第一个星期一是去年该月的第一个星期一,如何计算上一年的同一天 - How to calculate previous year same day of week from month suppose first Monday of month would be last year first Monday of that month 设置从一年的第一天到下一个星期一的一年的第一周 - Set first week of the year from the first day of the year until the following monday PostgreSQL:如何在SQL查询中将date_trunc的星期开始日期从星期一更改为另一天? - PostgreSQL: How change date_trunc week start day from monday to another day in sql query? 如何从 SQL 获取数周的数据。 周一第一天 - How to get data from SQL for weeks. First day Monday 获取一周的第一天 T-SQL - Get first day of week T-SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM