我创建了一个存储过程来获取每周结果。因此,使用两个输入参数(startDate,endDate),我可以得到我期望的结果。但是这里的问题是,当本周从2015年8月28日开始到2015年9月3日结束时,将获得28,29,30,31,32,33,34作为一周中的几天。但预期结果是28,29,30,31,01,02,03,04。
以下是我上述详细信息的代码。帮助我克服这个问题
ALTER PROCEDURE [dbo].[usp_Get_TimesheetDetails]
@UserID int, @startdate datetime, @enddate datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @intStartDate int
declare @intEndDate int
declare @strMonth varchar(50)
SELECT @intStartDate = DATEPART(day,@startDate)
Select @intEndDate = DATEPART(day,@endDate)
select @strMonth = DATENAME(MONTH, GETDATE())
declare @temptable table (num int)
declare @columns varchar(max)
DECLARE @sqlText nvarchar(1000);
DECLARE @startnum INT=@intStartDate-1
DECLARE @endnum INT=@intEndDate
;
WITH gen AS (
SELECT @startnum AS num
UNION ALL
SELECT num+1 FROM gen WHERE num+1<=@endnum
)
答案 0 :(得分:0)
您需要将开始日期和结束日期值作为日期传递,而不是作为整数传递。
试试这个......
SearchResponse response =
// Query creation part
client.prepareSearch("index1", "index2")
.setTypes("type1", "type2")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(QueryBuilders.termQuery("multi", "test"))
.setPostFilter(FilterBuilders.rangeFilter("age").from(12).to(18))
.setFrom(0).setSize(60).setExplain(true)
//query execution part
.execute()
.actionGet();
DECLARE @startnum DATE = '20150828' --<-- Use ANSI date `YYYYMMDD`
DECLARE @endnum DATE = '20150903'
;WITH gen AS (
SELECT @startnum AS num
UNION ALL
SELECT DATEADD(DAY,1,num) FROM gen
WHERE DATEADD(DAY,1,num) <= @endnum
)
SELECT * FROM Gen
答案 1 :(得分:0)
您需要传递开始日期和结束日期而不是整数值。试试这个
DECLARE @startnum DATE = '08/28/2015'
DECLARE @endnum DATE ='09/03/2015';
DECLARE @startnum DATE = '08/28/2015'
DECLARE @endnum DATE ='09/03/2015';
WITH gen
AS (SELECT @startnum AS DAT
UNION ALL
SELECT Dateadd(DD, 1, DAT)
FROM gen
WHERE DAT <= @endnum)
SELECT RIGHT('0' + Cast(Day(DAT) AS VARCHAR(2)), 2)
FROM GEN
注意:由于这里的日期范围非常小我使用了递归cte方法,否则你可以使用tally表来生成日期
答案 2 :(得分:0)
这是使用强类型日期但没有递归CTE循环的另一种解决方案。
WITH
t4 AS (SELECT n FROM (VALUES(0),(0),(0),(0)) t(n))
,t64 AS (SELECT ROW_NUMBER() OVER (ORDER BY (a.n)) - 1 AS num FROM t4 AS a, t4 AS b, t4 AS c)
SELECT DATEADD(day, num, @StartDate)
FROM t64
WHERE num <= DATEDIFF(day, @StartDate, @EndDate);