我在與下面的查詢問題:「ORDER BY」 SQL問題(超出範圍的日期/時間值)
SELECT
Consignments.LegacyID,
Consignments.TripDate,
Consignments.CollectionName,
CASE
WHEN Sage2.InvoiceSummaryType = 'HT' THEN DeliveryTown
ELSE DeliveryName + ', ' + DeliveryTown + ', ' + DeliveryPostCode END AS 'DeliveryName',
Consignments.Pallets,
Consignments.Weight,
Consignments.BaseRate,
Consignments.FuelSurcharge,
Consignments.AdditionalCharges,
Consignments.BaseRate * Consignments.Quantity AS 'InvoiceValue',
Consignments.InvoiceNumber,
Consignments.Customer
FROM
Consignments
INNER JOIN SageAccount
ON Consignments.Customer = SageAccount.LegacyID
AND SageAccount.Customer = 'true'
LEFT OUTER JOIN SageAccount AS Sage2
ON SageAccount.InvoiceAccount = Sage2.LegacyID
WHERE
(Sage2.Customer = 'true')
AND (Consignments.Customer = @Customer)
AND (Consignments.InvoiceNumber IS NOT NULL)
OR (Sage2.Customer = 'true')
AND (Consignments.InvoiceNumber IS NOT NULL)
AND (Sage2.InvoiceAccount = @Customer)
ORDER BY
CASE
WHEN Sage2.InvoiceSummaryType = 'HR' THEN TripDate
WHEN Sage2.InvoiceSummaryType = 'HS' THEN Consignments.LegacyID
END
出於某種原因,它不斷給我下面的錯誤:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value order by
但只有當它試圖Order By TripDate
,即當案「HR」發生。 TripDate是一個'日期時間字段'。
任何想法?
從某種角度來看,我猜你的懷疑是正確的。在集合包含兩種數據之前可能沒有問題。在那之後,你可能會說他們兩個都會導致問題,或者說他們都不會導致問題,只是一起使用它們而導致錯誤。事實上,正如你所說的那樣,避免混合使用數據類型,或者在將它們用於比較之前將它們強制爲一些常見類型。 +1 – 2010-12-02 13:24:19
感謝哥們,我試過了你的第二個問題,它的工作很完美。 – Chris 2010-12-02 15:04:20