Click here to Skip to main content
15,912,400 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

i would like to know how can i solve this error

Error converting data type nvarchar to datetime

when i run this procedure

SQL
alter proc Ledger

@Account_Serial int ,
@DateFrom datetime,
@DateTo datetime
as


SELECT     GLD_JOURNAL_ENTRY_DETAILES.ACCOUNT_SERILA_NO, SUM(GLD_JOURNAL_ENTRY_DETAILES.DEBIT_VALUE) AS DEBIT_VALUE, 
                      SUM(GLD_JOURNAL_ENTRY_DETAILES.CREDIT_VALUE) AS CREDIT_VALUE
FROM         GLD_JOURNAL_ENTRY_DETAILES INNER JOIN
                      GLD_JOURNAL_ENTRIES ON GLD_JOURNAL_ENTRY_DETAILES.JOURNAL_ENTRY_NO = GLD_JOURNAL_ENTRIES.Journal_ENTRY_NO
WHERE     (GLD_JOURNAL_ENTRIES.TRANSFER_DATE --BETWEEN @DateFrom AND @DateTo
between convert(datetime ,CONVERT(nvarchar(20),@DateFrom) ,102)
 and convert(datetime,CONVERT(nvarchar(20),@DateTo) ,105)
)

 
 
GROUP BY GLD_JOURNAL_ENTRY_DETAILES.ACCOUNT_SERILA_NO
HAVING      (GLD_JOURNAL_ENTRY_DETAILES.ACCOUNT_SERILA_NO = @Account_Serial)


any idea
Posted
Comments
Ganesh Raja 10-Dec-13 8:50am    
Already the @DateFrom and @DateTo in datetime format then why u r trying to convert again?

1 solution

Why are you passing DateTime values (sensible) and then converting them to the Sql default format (yyyy-MM-dd) and then trying to convert them back to a datetime as if they were in ANSI format (yy.MM.dd)?
Just use them directly as in your comments, or use CAST to remove the time portion:
SQL
WHERE GLD_JOURNAL_ENTRIES.TRANSFER_DATE BETWEEN CAST(@DateFrom AS DATE) AND CAST(@DateTo AS DATE)
 
Share this answer
 
Comments
Darsh_Basha 10-Dec-13 9:22am    
Hi @OriginalGriff

i tried ur answer

USE [CS_ATC]
GO

DECLARE @return_value int

EXEC @return_value = [dbo].[Ledger]
@Account_Serial = 4,
@DateFrom = N'26/1/2013',
@DateTo = N'1/1/2014'

SELECT 'Return Value' = @return_value

GO

and return this error

Error converting data type nvarchar to datetime.
OriginalGriff 10-Dec-13 9:27am    
Where did I suggest that?
I can't see anything like that suggested...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900