2

我正在尝试计算 SQL Server 2008 中两个日期之间状态为 0 的记录有我的程序,但它不是正确的 trows 错误。

消息 156,级别 15,状态 1,过程 sp_SerchPickupHispanicBetweenDates,第 6 行
关键字“between”附近的语法不正确。

程序看起来像

CREATE procedure sp_SerchPickupHispanicBetweenDates
@date1 date,
@date2 date
as 
select COUNT ( Pickuphispanic ) from  pickup
where  Pickuphispanic = 1 and pickupdate like between @date1+ '%' and @date1 +'%'

正在尝试在互联网上搜索解决方案,但没有成功。任何想法如何纠正它?我也有完美的选择语句

select COUNT ( Pickuphispanic ) from  pickup
where  Pickuphispanic = 1 and pickup.pickupdate between '2006-07-01' and '2010-12-31'
4

6 回答 6

4

和参数属于数据类型@date1,因此您不能将 a 附加到这些值(在使用运算符时,只能用于基于字符串的数据类型;另外:您不能使用值应用于搜索)。@date2DATE%%LIKELIKEDATE

改用这个:

CREATE procedure procSerchPickupHispanicBetweenDates
   @date1 date,
   @date2 date
AS
   SELECT COUNT (Pickuphispanic) 
   FROM dbo.pickup
   WHERE Pickuphispanic = 1 
     AND pickupdate BETWEEN @date1 AND @date2

要调用它,请使用以下语法:

EXEC procSerchPickupHispanicBetweenDates '2006-07-01', '2010-12-31'

(没有大括号,没有括号 - 只需指定两个日期),或者:

EXEC procSerchPickupHispanicBetweenDates @date1 = '2006-07-01', @date2 = '2010-12-31'
于 2013-02-19T09:18:44.623 回答
2

如果您尝试在 2 个日期之间进行搜索,您的存储过程必须如下所示:

pickupdate between @date1 and @date2

您不必使用 LIKE 功能

于 2013-02-19T09:19:35.030 回答
1

Like不能嵌套函数之间。

试试这个:

CREATE procedure sp_SerchPickupHispanicBetweenDates
@date1 date,
@date2 date
as 
select COUNT ( Pickuphispanic ) from  pickup
where  Pickuphispanic = 1 and CONVERT(VARCHAR(25), pickupdate , 126) like  @date1 + '%' and @date1 + '%'

问候

于 2013-02-19T09:20:00.283 回答
1

像这样修改查询:

SELECT COUNT ( Pickuphispanic_ID ) from  pickup
where  Pickuphispanic = 1 and pickupdate between @date1 AND @date2

或者

SELECT COUNT ( Pickuphispanic_ID ) from  pickup
where  Pickuphispanic = 1 and pickupdate> @date1 AND pickupdate<@date2
于 2013-02-19T09:20:24.693 回答
0

Like 不能嵌套在函数之间。

如果你想使用 like 那么它应该是这样的

CREATE procedure sp_SerchPickupHispanicBetweenDates
@date1 date,
@date2 date
as 
select COUNT ( Pickuphispanic ) from  pickup
where  Pickuphispanic = 1 and (CONVERT(VARCHAR, pickupdate ,106) like  CONVERT(VARCHAR,@date1,106) + '%' or CONVERT(VARCHAR, pickupdate ,106) like   CONVERT(VARCHAR,@date2,106) + '%')

你想在这之间使用它应该是这样的

CREATE procedure procSerchPickupHispanicBetweenDates
   @date1 date,
   @date2 date
AS
   SELECT COUNT (Pickuphispanic) 
   FROM dbo.pickup
   WHERE Pickuphispanic = 1 
     AND Convert(date,pickupdate) BETWEEN Convert(date,@date1) AND Convert(date,@date2)
于 2015-08-31T11:19:37.850 回答
0

ISNULL(CONVERT(DATE,@FromDate),'2000-01-01') 和 ISNULL(CONVERT(DATE,@ToDate),'3000-01-01')) 之间的 FilterDate

于 2020-01-18T18:02:21.447 回答