我需要结果中的所有日期排除星期日:
输入:开始日期= 2016-07-01和结束日期= 2016-07-31
S_No Dates
1 01-07-2016
2 02-07-2016
3 04-07-2016
4 05-07-2016
5 06-07-2016
6 07-07-2016
7 08-07-2016
8 09-07-2016
9 11-07-2016
10 12-07-2016
11 13-07-2016
12 14-07-2016
13 15-07-2016
14 16-07-2016
15 18-07-2016
16 19-07-2016
17 20-07-2016
18 21-07-2016
19 22-07-2016
20 23-07-2016
21 25-07-2016
22 26-07-2016
23 27-07-2016
24 28-07-2016
25 29-07-2016
26 30-07-2016
我需要这样的结果。如果我输入输入:Start Date = 2016-06-01 and End Date = 2016-06-30
。所有日期都应该在2016年6月排除星期日。
请帮帮我?
答案 0 :(得分:0)
您可以使用给定的查询
此dw
函数中的datepart()
表示"周日"。
你想要排除星期日,所以在where子句的条件中写1。
查询:
select S_No, Dates
from table_name
where datepart(dw,Dates) <>1 and Dates >='2016-06-01' and Dates<='2016-06-30'
因此,根据此查询,您将获得2016-06-01
和2016-06-30
之间的所有日期,不包括星期日。
答案 1 :(得分:0)
如果您想要一个返回不包括星期日的日期的查询,请尝试以下方法:
declare @dateStart date
declare @dateEnd date
set datefirst 7 -- Setting Sunday as firstday of the week
set @dateStart = cast('2016-06-01' as date) -- Maybe as paramters?
set @dateEnd = cast('2016-06-30' as date)
;WITH T AS (SELECT @dateStart as d union all
SELECT DATEADD(DAY,1,d) from T where d<@dateEnd)
select *
from T
where datepart(WEEKDAY,d) <> 1
<强>输出强>
d
2016-06-01
2016-06-02
2016-06-03
2016-06-04
2016-06-06
2016-06-07
2016-06-08
2016-06-09
2016-06-10
2016-06-11
2016-06-13
2016-06-14
2016-06-15
2016-06-16
2016-06-17
2016-06-18
2016-06-20
2016-06-21
2016-06-22
2016-06-23
2016-06-24
2016-06-25
2016-06-27
2016-06-28
2016-06-29
2016-06-30
答案 2 :(得分:0)
创建一个整数表:
SELECT TOP 1000000
IDENTITY(INT,1,1) as N
INTO dbo.NumbersTable
FROM Master.dbo.SysColumns
创建日期范围表
DECLARE @DaysFromStart int
DECLARE @StartDate datetime
SET @StartDate = '10/01/2008'
SET @DaysFromStart = (SELECT (DATEDIFF(dd,@StartDate,GETDATE()) + 1))
CREATE TABLE [dbo].[TableOfDates](
[fld_date] [datetime] NOT NULL,
CONSTRAINT [PK_TableOfDates] PRIMARY KEY CLUSTERED
(
[fld_date] ASC
)WITH FILLFACTOR = 99 ON [PRIMARY]
) ON [PRIMARY]
INSERT INTO
dbo.TableOfDates
SELECT
DATEADD(dd,nums.n - @DaysFromStart ,CAST(FLOOR(CAST(GETDATE() as FLOAT)) as DateTime)) as FLD_Date
FROM dbo.NumbersTable nums
SELECT MIN(FLD_Date) FROM dbo.TableOfDates
SELECT MAX(FLD_Date) FROM dbo.TableOfDates
您要使用的最终查询:
SELECT FLD_Date FROM TableOfDates (NOLOCK) WHERE FLD_Date >= '2016-07-01' AND FLD_Date <= '2016-07-31' AND DATENAME(dw,FLD_Date) <> 'Sunday'
答案 3 :(得分:0)
检查一下
WITH mycte AS
(
SELECT CAST('2016-06-01' AS DATETIME) DateValue
UNION ALL
SELECT DateValue + 1
FROM mycte
WHERE DateValue + 1 < '2016-07-01'
)
SELECT DateValue
FROM mycte
WHERE ((DATEPART(dw, DateValue) + @@DATEFIRST) % 7) NOT IN (1)
OPTION (MAXRECURSION 0);
输出将是......
DateValue
2016-06-01 00:00:00.000
2016-06-02 00:00:00.000
2016-06-03 00:00:00.000
2016-06-04 00:00:00.000
2016-06-06 00:00:00.000
2016-06-07 00:00:00.000
2016-06-08 00:00:00.000
2016-06-09 00:00:00.000
2016-06-10 00:00:00.000
2016-06-11 00:00:00.000
2016-06-13 00:00:00.000
2016-06-14 00:00:00.000
2016-06-15 00:00:00.000
2016-06-16 00:00:00.000
2016-06-17 00:00:00.000
2016-06-18 00:00:00.000
2016-06-20 00:00:00.000
2016-06-21 00:00:00.000
2016-06-22 00:00:00.000
2016-06-23 00:00:00.000
2016-06-24 00:00:00.000
2016-06-25 00:00:00.000
2016-06-27 00:00:00.000
2016-06-28 00:00:00.000
2016-06-29 00:00:00.000
2016-06-30 00:00:00.000
答案 4 :(得分:0)
您的问题的解决方案如下:
DECLARE @START_DATE DATETIME='2:57 PM 7/01/2016',@END_DATE DATETIME='2:57 PM 7/26/2016'
WHILE(@START_DATE<=@END_DATE)
BEGIN
IF(DATENAME(WEEKDAY,@START_DATE)<>'Sunday')
BEGIN
PRINT @START_DATE --Modify code according to your need.
END
SELECT @START_DATE=DATEADD(D,1,@START_DATE)
END
结果:
Jul 1 2016 2:57PM
Jul 2 2016 2:57PM
Jul 4 2016 2:57PM
Jul 5 2016 2:57PM
Jul 6 2016 2:57PM
Jul 7 2016 2:57PM
Jul 8 2016 2:57PM
Jul 9 2016 2:57PM
Jul 11 2016 2:57PM
Jul 12 2016 2:57PM
Jul 13 2016 2:57PM
Jul 14 2016 2:57PM
Jul 15 2016 2:57PM
Jul 16 2016 2:57PM
Jul 18 2016 2:57PM
Jul 19 2016 2:57PM
Jul 20 2016 2:57PM
Jul 21 2016 2:57PM
Jul 22 2016 2:57PM
Jul 23 2016 2:57PM
Jul 25 2016 2:57PM
Jul 26 2016 2:57PM
答案 5 :(得分:0)
SELECT * FROM tableName 在2016-06-01 03:13:45.040'''2016-06-30 03:13:45.040'和DATEPART(dw,日期)&lt;&gt; 1
之间的日期