मेरे पास SQL सर्वर 2005 में एक SQL क्वेरी है जो टूट रही है जब मैं एक सशर्त आदेश शामिल करता हूं। जब मैं ऑर्डर हटा देता हूं, तो क्वेरी काम करती है। जब मैं स्पष्ट रूप से स्थिति द्वारा आदेश लिखता हूं (उदाहरण के लिए पी। डिस्क्रिप्शन द्वारा ऑर्डर) यह काम करता है। जब मैं द्वारा सशर्त आदेश में शामिल हैं, मैं त्रुटि,एसक्यूएल सर्वर सशर्त आदेश
'Conversion failed when converting character string to smalldatetime data type'
एसक्यूएल सर्वर मुझे नहीं दिखा रहा है जो कोड की पंक्ति इस त्रुटि के कारण मिलता है। मैं सोच रहा हूं कि मैं इसे कैसे ठीक कर सकता हूं ताकि मैं सशर्त आदेश का उपयोग कर सकूं या समस्या निवारण कर सकूं जो रूपांतरण में विफल हो रहा है।
declare @SearchTerm nvarchar(255)
declare @SortBy nvarchar(255)
declare @Months int
declare @VendorID int
declare @ProductID int
set @SearchTerm = 'focus'
set @SortBy = 'product'
set @Months = 3
set @VendorID = null
set @ProductID = null
-- This makes it so the @Month will filter by n number of months ago.
declare @PreviousMonths datetime
if @Months is null
begin
set @PreviousMonths = 24
end
else
begin
set @PreviousMonths = DateAdd(month, [email protected], GetDate())
end
select
a.dsAlertID as AlertID,
a.ProductID,
v.VendorID,
p.Description as ProductName,
v.LongName as VendorName,
a.Introduction,
a.Writeup,
a.DateAdded
from
ev_ds_Alerts a
left outer join
tblProducts p on a.ProductID = p.ProductID
left outer join
tblVendors v on v.VendorID = p.VendorID
where
(@SearchTerm is null or (a.Writeup like '% ' + @SearchTerm + '%' or a.Introduction like '% ' + @SearchTerm + '%'))
and ((@Months is null) or (@Months is not null and a.DateAdded >= @PreviousMonths))
and ((@VendorID is null) or (@VendorID is not null and v.VendorID = @VendorID))
and ((@ProductID is null) or (@ProductID is not null and p.ProductID = @ProductID))
order by
case @SortBy
when 'product' then p.Description
when 'vendor' then v.LongName
else a.DateAdded
end
-- order by p.Description or v.LongName works when explicitly writing them out!
आप n हूँ 'CAST' –
का उपयोग करने के लिए eed डेटा प्रकार कास्ट त्रुटि –