我有這樣的存儲過程。如果我試圖在6月1日至7月31日執行它,則會顯示轉換錯誤。任何人都可以請幫助如何解決這個問題?它顯示了7月6日和7月1日的結果正常工作。但如果我7月31日使用它不起作用。如何解決「錯誤轉換數據類型varchar到日期」
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[Samp]
(@StartDate date ,
@EndDate date,
@Flowid int)
AS
BEGIN
if(@Flowid = 909)
begin
select
COUNT(distinct a.Audit_id)
from
[Prod_Workflow_Client41].[dbo].[CustomValues909] a
join
[Prod_Workflow_Client41].[dbo].[Audit] b on a.id = b.id
join
[Prod_Workflow_Client41].[dbo].[Substatus] c on c.Id = b.Substatus_id
where
c.MajorStatus <> 'Cancelled'
and a.Auditor_Reviewed_Date >= @StartDate
and a.Auditor_Reviewed_Date <= @EndDate
end
else if @Flowid = 1010
begin
select
COUNT(distinct a.Audit_id)
from
[Prod_Workflow_Client41].[dbo].[CustomValues1010] a
join
[Prod_Workflow_Client41].[dbo].[Audit] b on a.id = b.id
join
[Prod_Workflow_Client41].[dbo].[Substatus] c on c.Id = b.Substatus_id
where
c.MajorStatus <> 'Cancelled'
and a.Auditor_Reviewed_Date >= @StartDate
and a.Auditor_Reviewed_Date <= @EndDate
end
else if @Flowid = 101
begin
select
COUNT(distinct a.Audit_id)
from
[Prod_Workflow_Client41].[dbo].[CustomValues101] a
join
[Prod_Workflow_Client41].[dbo].[Audit] b on a.id = b.id
join
[Prod_Workflow_Client41].[dbo].[Substatus] c on c.Id = b.Substatus_id
where
c.MajorStatus <> 'Cancelled'
and a.Auditor_Reviewed_Date >= @StartDate
and a.Auditor_Reviewed_Date <= @EndDate
end
end
--exec [dbo].[Samp] '01/06/2015','31/06/2015',909
--exec [dbo].[Samp] '01/06/2015','30/06/2015',1010
--exec [dbo].[Samp] '01/06/2015','30/06/2015',101
你應該嘗試把你的代碼分解到導致問題的部分 –
a.Auditor_Reviewed_Date列是nvarchar(max)數據類型.. – Perumal
[踢壞壞習慣:選擇錯誤的數據類型](http:// sqlblog .com/blogs/aaron_bertrand/archive/2009/10/12/bad-habits-to-kick-using-the-wrong-data-type.aspx) - 你應該總是使用最合適的數據類型 - 畢竟是在那裏!如果你有一個**日期** - 你應該**這樣存儲它!**不要將日期保存爲'varchar',絕對不是'varchar(max)'(或者你有沒有看過日期** ** –