gpt4 book ai didi

sql - 如何在 2 字符串日期之间使用命令

转载 作者:行者123 更新时间:2023-12-04 20:36:38 25 4
gpt4 key购买 nike

我正在使用以下选择查询,但问题是 IssueDate 列在 varchar 中,它没有给出正确的结果。在 SQL 中的命令字符串日期之间使用任何方式:

SELECT Pkid AS [Bill Id],
InvoiceNumber,
InvoiceType,
IssueDate,
FinalTotal
FROM tblInvoiceMain
WHERE IssueDate BETWEEN '01 June 2016' AND '06 June 2016'
ORDER BY Pkid DESC

输出:

enter image description here

最佳答案

SELECT Pkid AS [Bill Id],
InvoiceNumber,
InvoiceType,
IssueDate,
FinalTotal
FROM tblInvoiceMain
WHERE CAST(IssueDate AS DATE) BETWEEN '20160601' AND '20160606'
ORDER BY Pkid DESC

如果执行时间会非常长:

ALTER TABLE dbo.tblInvoiceMain
ADD IssueDate2 AS CAST(IssueDate AS DATE)
GO
CREATE NONCLUSTERED INDEX ix
ON dbo.tblInvoiceMain (IssueDate2)
INCLUDE (Pkid, InvoiceNumber, InvoiceType, IssueDate, FinalTotal)
GO

SELECT Pkid AS [Bill Id],
InvoiceNumber,
InvoiceType,
IssueDate,
FinalTotal
FROM tblInvoiceMain
WHERE IssueDate2 BETWEEN '20160601' AND '20160606'
ORDER BY Pkid DESC

关于sql - 如何在 2 字符串日期之间使用命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37652421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com