Sql选择2个日期之间的查询

时间:2017-03-08 09:36:06

标签: c# sql-server

您好我使用此存储过程来获取一些信息:

ALTER proc [dbo].[RevenusStaticAdvanced]
@Days int
as
-------------------------------------------------------------------------------------
select ISNULL(SUM(CAST(TotalPrice as int)),0) as 'TotalPrice',
 ISNULL(COUNT(Lavage_Orders.ID),0) as 'Total Commandes'
from Lavage_Orders 
inner join LavageTypes on Lavage_Orders.LavageType=LavageTypes.ID
WHERE DATEDIFF(Day,Arrive,GETDATE()) Between 0 and @Days
------------------------------------------------------------------------------------------------
select ISNULL(COUNT(ID),0) as 'TotalCommandes' , ISNULL(SUM(CAST(TotalPrice as int)),0) as 'RevnusRepairs' 
from Repair_OrdersDetails 
WHERE DATEDIFF(Day,Date,GETDATE()) Between 0 and @Days
------------------------------------------------------------------------------
select ISNULL(SUM(Qte),0) as 'SelledQte' , ISNULL(COUNT(ID),0) as 'TotalCommandes' , ISNULL(SUM(CAST(TotalPrice as int)),0) as 'RevnusAccessoires'
 from Accessoires_Order 
inner join Accessoires_OrderDetails on Accessoires_OrderDetails.orderID=Accessoires_Order.ID
WHERE DATEDIFF(Day,Date,GETDATE()) Between 0 and @Days
------------------------------------------------------------------------------------------------------
select ISNULL(Accessoires.ID,0), ISNULL(SUM(Accessoires_OrderDetails.Qte),0) as Selled from Accessoires
inner join Accessoires_OrderDetails on Accessoires_OrderDetails.AccessoireID=ID
inner join Accessoires_Order on Accessoires_Order.ID=Accessoires_OrderDetails.orderID
WHERE DATEDIFF(DAY,Accessoires_Order.Date,GETDATE()) Between 0 and @Days
GROUP BY Accessoires.ID
ORDER BY Selled
DESC

当我给它一个天数(从当前日)时,这非常有效 所以我想将它更改为2个日期之间所以我将条件更改为:

WHERE Date Between @Date1 and @Date2

但这似乎没有用。

我在c#中传递日期值,如:

public AdvancedStatics AdvancedStaticsView2(DateTime D1,DateTime D2)
{
        param[0] = new SqlParameter("@Date1", SqlDbType.DateTime);
        param[0].Value = D1;
        param[1] = new SqlParameter("@Date2", SqlDbType.DateTime);
        param[1].Value = D2;
}

我桌上的存储日期如下:

2017-01-11 14:20:48.177

1 个答案:

答案 0 :(得分:0)

您的参数与存储过程参数不匹配。

如果您想要过几天:

public AdvancedStatics AdvancedStaticsView2(DateTime D1,DateTime D2)
{
        param[0] = new SqlParameter("@Days", SqlDbType.Int);
        param[0].Value = 45;
}

如果您想传递两个日期:

ALTER proc [dbo].[RevenusStaticAdvanced]
(
    @Date1 DateTime,
    @Date2 DateTime,
)
as