Operand data type varchar is invalid for sum operator.

Analyst_SQL 3,576 Reputation points
2021-01-23T10:36:07.06+00:00

i am doing sum of Trans_Amount ,but error is coming.

;with cte as (

SELECT T.Trans_ID as ID,COnvert(varchar,T.Trans_Date,101) as Date,COA.Level_Four_Name as Head,T.Cheque_No ,
 Convert(varchar,T.Cheque_Date,101) as Cheque_Date,T.Cheque_Bank,T.Cheque_Branch


,(CONVERT(varchar(50), CAST(t.Trans_Amount AS money),1)) as Amount,T.Cheque_Status 
 FROM tbl_transection T
 inner join tbl_COA COA on COA.Level_Four_ID=T.Level_Four_ID_C
 where T.Trans_Type_ID=2 )
 select 'Total' ,SUM((CONVERT(varchar(50), CAST(cte.Amount AS money),1))) from cte
Developer technologies | Transact-SQL
SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. Erland Sommarskog 122K Reputation points MVP Volunteer Moderator
    2021-01-23T10:40:48.043+00:00

    The error message is pretty clear isn't it? Your CTE returns a string and then you are trying to perform an arithmetic operation on a string. I mean what is the sum of New York, London Paris and Munich?

    I think you should ask yourself why you put in that convert in the CTE in the first place.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.