2017-06-21 37 views
0

我想創建一個視圖。視圖中的一個字段是smalldatetime字符類型。我正試圖在特定日期範圍內顯示記錄。似乎有一個字符類型轉換問題。我得到的錯誤是「將varchar數據類型轉換爲smalldatetime數據類型導致超出範圍的值。」SQL VIEWS /數據類型轉換

這裏是我的一段代碼

CREATE VIEW erep 
AS 
SELECT [iInvoiceID] as invoice_id 
     ,[OrderDate] as rrdate 
     ,[cDescription] as product 
     ,[fQtyProcessed] as qty 
     ,[fUnitPriceExcl] as unitpricenotax 
     ,[fUnitPriceIncl] as unitpricewithtax 
     ,[fUnitCost] as unitcost 
     ,[fTaxRate] as taxrate 
     ,[fQuantityLineTotIncl] as totalwithtax 
     ,[fQuantityLineTotExcl] as totalnotax 
     ,[fQuantityLineTotInclNoDisc] as totalwithtaxnodiscount 
     ,[fQuantityLineTotExclNoDisc] as totalnotaxnodisc 
     ,[iDeliveryStatus] as deliverystatus 
     FROM _bvSalesOrdersFull 
    where orderdate >= '2017-01-01 00:00:00' AND orderdate < '2017-31-12 00:00:00'; 
+1

它可能是一個時區問題。它是否提供了orderdate列? –

回答

0

爲了您的日期時間文字,使用格式是明確的,不管是什麼語言/文化設置:

CREATE VIEW erep 
AS 
SELECT [iInvoiceID] as invoice_id 
     ,[OrderDate] as rrdate 
     ,[cDescription] as product 
     ,[fQtyProcessed] as qty 
     ,[fUnitPriceExcl] as unitpricenotax 
     ,[fUnitPriceIncl] as unitpricewithtax 
     ,[fUnitCost] as unitcost 
     ,[fTaxRate] as taxrate 
     ,[fQuantityLineTotIncl] as totalwithtax 
     ,[fQuantityLineTotExcl] as totalnotax 
     ,[fQuantityLineTotInclNoDisc] as totalwithtaxnodiscount 
     ,[fQuantityLineTotExclNoDisc] as totalnotaxnodisc 
     ,[iDeliveryStatus] as deliverystatus 
     FROM _bvSalesOrdersFull 
    where orderdate >= '2017-01-01T00:00:00' AND orderdate < '2017-12-31T00:00:00'; 

,但不是我會預計的終點是'2018-01-01T00:00:00',除非它的意圖排除發生的訂單期間 12月的最後一天呃。

明確的日期格式是YYYYMMDD,YYYY-MM-DD'T'hh:mm:ssYYYY-MM-DD'T'hh:mm:ss.mil。一些其他格式可能這些天工作得很好,但歷史上SQL Server可能會與他們混淆;值得注意的是YYYY-MM-DD曾經有問題,我相信。就像使用空格作爲日期/時間分隔符一樣。