简体   繁体   中英

How to fix the SQL error "Error converting data type varchar to numeric"?

I am selecting records from a SQL Server database but result says that the query completed with errors and then error I get is

Error converting data type varchar to numeric

In my query I am fetching records based on particular codes and then I use the = the query fails but using <> the query runs. Can anyone assist me where I am doing it wrong because I looked for solutions provided they are not working for me.

I am sharing by query below

SELECT
    MAX(amount) AS amount, 
    MAX(date_of_payment) AS date_of_payment, 
    MAX(customer) AS customer, 
    MAX(payment_type) AS payment_type, 
    MAX(currency) AS currency, 
    MAX(PayCtrNum) AS PayCtrNum, 
    MAX(TRACKING_NO) AS TRACKING_NO, 
    MAX(Payment_id) AS Payment_id 
FROM 
    PAYMENTS_VW 
WHERE 
    date_of_payment = '21/01/2020' 
    AND account_code = '440342' 
GROUP BY 
    Payment_id

I have used CAST and changed the column to Int it is working now. The query now reads

SELECT max(amount) as amount, 
max(date_of_payment) as date_of_payment, 
max(customer) as customer, 
max(payment_type) as payment_type, 
max(currency) as currency, 
max(PayCtrNum) as PayCtrNum, 
max(TRACKING_NO) as TRACKING_NO, 
max(Payment_id) as Payment_id 
FROM PAYMENTS_VW 
WHERE date_of_payment = '21/01/2020' 
AND CAST(account_code AS int) = '440342' 
GROUP BY Payment_id

And it is working

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM