2013-02-20 87 views

回答

1

第一關:

  • 你有什麼值@Year和@ContractDate?
  • 什麼格式/類型是@ContractDate?

我問,因爲這對我的作品us_english SQL Server上2012

SELECT cast('2008/02/29' as datetime) 

但是,如果我改變SET DATEFORMAT然後我可以打破上面的代碼

-- breaks 
SET DATEFORMAT DMY; 
SELECT cast('2008/02/29' as datetime) 

使用ISO日期格式爲好的不管我用什麼SET DATEFORMAT

SET DATEFORMAT DMY; 
SELECT cast('20080229' as datetime); 
SET DATEFORMAT MDY; 
SELECT cast('20080229' as datetime); 
SET DATEFORMAT YDM; 
SELECT cast('20080229' as datetime); 

結論:您使用非ISO日期格式和依靠隱式轉換

+0

有沒有對任何設置的任何值。我已經做了一個單獨的查詢,我設置@年= 2012年和ContractDate ='2008-02-29 00:00:00.000'但我沒有在存儲過程中設置它。不確定你的意思是什麼格式ContractDate?你的意思是數據類型? – user2091766 2013-02-20 15:32:53

0

這是更好地轉換爲日期時間/日期之前獲得ISO格式(yyyymmdd)的字符串,它會在任何服務器工作不管區域性設置

如果@ContractDatedatetime/date類型和@year就像2012/'2012'然後下面應該工作。 Working Example

Set @ContractDate = convert(datetime, 
    convert(varchar(4),@year) + right(convert(varchar(8),@ContractDate,112),4)) 
0
ALTER PROCEDURE [dbo].[Sden_report1] 
@fromdt nvarchar(13), 
@todt nvarchar(13) 

AS 

BEGIN 
select AD_PID,AD_Appointmentdate,AD_Patientname,AD_Patientphno,AD_Visitpurpose 
from Appointment_Details1 
where 
--PL_CID = @CID and PL_Itemcode = @JCode AND 
(@fromdt = '' OR (DATEDIFF(DAY,AD_Appointmentdate, @fromdt) <= 0)) 
AND (@todt = '' OR (DATEDIFF(DAY,AD_Appointmentdate, @todt) >= 0)) 
end 


GO 

When i Execute the procedure 
i will get below error: 

EXEC Sden_report1 '20-11-2012','20-05-2013' 

Error 
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value. 

Anyone Help...`enter code here` 
相關問題