Crystal Reports-将Month和Year参数转换为Command WHERE子句的开始和结束日期

问题描述 投票:0回答:1

我需要在Crystal Reports中创建Month和Year参数,并将其转换为Command WHERE子句的开始和结束日期。我不能将Select Expert与公式一起使用,因为报表会检索很多数据,因此我需要先限制数据,然后再将其放入CR。

我有此SQL语句可将varchar月份和年份转换为开始日期和结束日期:

DECLARE @month varchar(2)
DECLARE @year varchar(4)

set @month= '12'
set @year= '2019'

select 
CAST(cast(@year + '-' + Cast(cast(@month as int) as varchar(20)) + '-01' as datetime) as DATE) as StartDate
,DATEADD(dd, -DAY(DATEADD(mm, 1, cast(@year + '-' + @month + '-01' as date))), DATEADD(mm, 1, cast(@year + '-' + @month + '-01' as date))) as EndDate

然后我的CR命令将类似于:

select * from Table1 where Date between StartDate and EndDate

问题是我无法弄清楚如何在CR中创建参数来实现此功能。当我将它们创建为字符串数据类型时,出现错误:

enter image description here

我不能让用户输入开始日期和结束日期,都需要月份和年份!

感谢您的帮助。

crystal-reports crystal-reports-2008
1个回答
0
投票

对我有用的答案是采用水晶数参数并将其转换为varchar SQL变量。 CR参数是下面的'{?month}'和'{?year}。

declare
@StartDate datetime,
@EndDate datetime

set @StartDate = cast(cast({?year} as varchar(4)) + '-' + cast({?month} as varchar(2)) + '-01' as date) 
set @EndDate = DATEADD(dd, -DAY(DATEADD(mm, 1, cast(cast({?year} as varchar(4)) + '-' + cast({?month} as varchar(2)) + '-01' as date))), DATEADD(mm, 1, cast(cast({?year} as varchar(4)) + '-' + cast({?month} as varchar(2)) + '-01' as date)))

select * from Table1 where Date bewteen @StartDate and @EndDate
© www.soinside.com 2019 - 2024. All rights reserved.