sourabh choubey

sourabh choubey

  • NA
  • 174
  • 46.1k

casting to datetime

Jul 18 2016 5:30 AM
here is mah query..
 
USE [BMS]
GO
/****** Object: StoredProcedure [dbo].[sp_SearchParticularsDetailsByPivot] Script Date: 18-07-2016 14:15:14 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[sp_SearchParticularsDetailsByPivot]
(
@fromdate datetime,
@todate datetime
)
as
begin
declare @expenseNames nvarchar(max)='';
--select @expenseNames ='[Building_EXPENSES], [eLECTRIC]'
--select @expenseNames= case when datalength(@expenseNames) = 0 then '' else ', ' end + '[' + ltrim(rtrim(Expenses_Name)) + ']'
--from expenses
(select @expenseNames +=
+'['+Expenses_Name+']'
+','
FROM dbo.Expenses)
set @expenseNames=(SELECT LEFT(@expenseNames, (LEN(@expenseNames)-1)))
declare @dynamicSQL nvarchar(max)='';
select @dynamicSQL =
'select * from
(select dbo.CreditDebit.ID AS SLNO, TransactionDate, Particular,Expenses_Name,Debit_Expenses,Credit_Expenses,Expenses,
Balance from dbo.CreditDebit inner join
dbo.CreditExpenses on dbo.CreditDebit.ID=dbo.CreditExpenses.Credit_ID
inner join dbo.Expenses on dbo.CreditExpenses.Expenses_ID=dbo.Expenses.ID
where dbo.CreditDebit.TransactionDate between cast("'+@fromdate+'",datetime) and cast("'+@todate+'",datetime) )src
pivot
(
sum(Expenses)
for Expenses_Name In ('+@expenseNames+')
)as pvt'
EXEC (@dynamicSQL)
end
 
when i execute this query i got error as
Msg 241, Level 16, State 1, Procedure sp_SearchParticularsDetailsByPivot, Line 20
Conversion failed when converting date and/or time from character string.
 
 

Answers (19)