我写了一个查询来从数据库中获取今天的数据。
SqlDataAdapter da = new SqlDataAdapter(@"SELECT
Bill_Date as [Bill Date],
Customers.Name as [Customer Name],
Item, Item_Code as [Item Code],
MRP, Tax_Percentage as [Tax Percentage],
Quantity, Amount as Total,
Discount, Amount_After_Discount as [Grand Total],
Billing_Type as [Billing Type],
Transaction_Number as [Transaction Number]
FROM
POS
LEFT JOIN
Customers ON POS.Customer = Customers.Customer_Id
WHERE
Bill_Date = CAST(GETDATE() AS DATE)", con);
我正在将结果显示到datagridview。但没有任何反应。没有检索到任何数据。但今天的数据库中有数据。
答案 0 :(得分:3)
这是您的查询:
SELECT . . .
FROM POS LEFT JOIN
Customers
ON POS.Customer=Customers.Customer_Id
WHERE Bill_Date = CAST(GETDATE() AS Date);
我可以想到为什么这不会返回任何东西的三个原因:
Bill_Date
包含时间和日期。Bill_Date
以字符串形式存储,无法转换为日期。你可以很容易地检查最后两个。对于第二个:
WHERE cast(Bill_Date as date) = CAST(GETDATE() AS Date);
第三个:
select bill_date
from <whatever>
where try_convert(date, bill_date) is null;
答案 1 :(得分:0)
日期是时间点,因此除非您在创建记录时从记录中删除了任何时间部分,否则在查询数据时需要考虑这一点。 通常我使用形式&gt; = date和&lt; (日期+ 1天)因为它捕获一天中的所有时间点。在您的示例中,这将转换为:
WHERE Bill_Date >= CAST(GETDATE() AS Date) AND Bill_Date < CAST((GETDATE() + 1) AS Date)