从字符串转换日期和/或时间时转换失败

时间:2013-07-02 17:12:54

标签: c# sql sql-server sql-server-2008

此查询在Sql Server Management Studio中运行正常,但是当我尝试在数据集&中运行时从日期时间选择器传递日期(@ Param2& @ Pram3),它抛出异常转换失败。我不知道这有什么不对,我的其他查询工作正常。这是因为UNION ALL发生了吗?

.NET代码

startDatePicker.Format = DateTimePickerFormat.Short;
endDatePicker.Format = DateTimePickerFormat.Short;
DataTable ntwTable = ntw.GetData(owner,start,end);
start = startDatePicker.Value.ToShortDateString();
end = endDatePicker.Value.ToShortDateString();

SQL

SELECT        o.Name, SUM(sq.SQuantity * sq.SRate) AS TotalSale, SUM(sq.PrQuantity * sq.PrRate) AS TotalPurchase, (SUM(sq.PQuantity) - SUM(sq.SQuantity)) 
                                 * (SUM(sq.PQuantity * sq.PRate) / SUM(sq.PQuantity)) AS Inventory
        FROM            (SELECT        OwnerId, CompanyId, Quantity AS PQuantity, RatePerShare AS PRate, 0 AS SQuantity, 0 AS SRate, 0 AS PrQuantity, 0 AS PrRate, Date
                                  FROM            Purchase
                                  UNION ALL
                                  SELECT        OwnerId, CompanyId, 0 AS PQuantity, 0 AS PRate, 0 AS SQuantity, 0 AS SRate, Quantity AS PrQuantity, RatePerShare AS PrRate, Date
                                  FROM            Purchase AS pr
                                  UNION ALL
                                  SELECT        OwnerId, CompanyId, Quantity AS PQuantity, 0 AS PRate, 0 AS SQuantity, 0 AS SRate, 0 AS PrQuantity, 0 AS PrRate, Date
                                  FROM            Bonus
                                  UNION ALL
                                  SELECT        OwnerId, CompanyId, Quantity AS PQuantity, CostOfShare AS PRate, 0 AS SQuantity, 0 AS SRate, 0 AS PrQuantity, 0 AS PrRate, Date
                                  FROM            RightShare
                                  UNION ALL
                                  SELECT        OwnerId, CompanyId, 0 AS PQuantity, 0 AS PRate, Quantity AS SQuantity, RatePerShare AS SRate, 0 AS PrQuantity, 0 AS PrRate, Date
                                  FROM            Sales) AS sq INNER JOIN
                                 Owner AS o ON o.OwnerId = sq.OwnerId
        WHERE        (o.OwnerId = @Param1) AND (sq.Date BETWEEN @Param2 AND @Param3)
        GROUP BY sq.CompanyId, o.Name

3 个答案:

答案 0 :(得分:1)

在您的代码中,有两种(显而易见的)失败的可能性。

第一个是date列中的隐式转换错误。如果purchase.date是日期/时间值,但union all中的一列不是:bonus.date,则权利共享。, or sales.date {{1}在SSMS中联合all`查询以查看是否发生错误。

第二种可能性是. To be honest, I would be surprised if this is the case. However, it is easy enough to check for. Just run the@Param1出错。这些似乎是作为字符串传递的。您可以通过查看字符串并执行以下操作来测试:

@Param2

一个可能的问题是操作系统和数据库级别的不同日期格式。例如,在2013年1月20日可能是1月20日,另一个是错误。如果这是问题,您可以使用select cast(@Param1 as datetime), cast(@Parame2 as datetime) 将参数转换为正确的格式(在数据库中)或在传递参数之前进行转换。

答案 1 :(得分:0)

我确定这不是最佳答案,但是,这似乎是由于区域设置。如果您更改以下两行:

start = startDatePicker.Value.ToShortDateString();
end = endDatePicker.Value.ToShortDateString();

要:

start = startDatePicker.Value.ToString("yyyy-MM-dd HH:mm:ss")
end = endDatePicker.Value.ToString("yyyy-MM-dd HH:mm:ss")

然后我相信这应该可以正常工作。

答案 2 :(得分:0)

(CONVERT(CHAR(10),sq.Date,111)@Waram2和@Param3之间

只需转换。