Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Error converting data type nvarchar to numeric.

What I have tried:

SQL
select MAX(cast(number_qamema as decimal(15,4))) as NB_QE from DB_DAEN where name_costomer1='waleed'  group by name_costomer1
Posted
Updated 22-Jan-18 13:44pm
v2
Comments
PIEBALDconsult 22-Jan-18 17:58pm    
We'd need to see some sample data.
CHill60 23-Jan-18 4:25am    
Hint: Don't store numbers (or dates) as varchar columns. Use the correct column type.

1 solution

The error is caused by your data - the column number_qanema cannot be converted to a decimal value.
Change your query as follows;
SQL
SELECT
    MAX(CAST(number_qamema AS DECIMAL(15,4))) AS NB_QE 
FROM
    NB_QE
WHERE
    ISNUMERIC(number_qamema) = 1
    AND
    name_costomer1 = 'waleed'
GROUP BY
    name_costomer1


Kind Regards
 
Share this answer
 
Comments
Richard Deeming 23-Jan-18 12:05pm    
Be careful with ISNUMERIC - it considers some strings as numeric which can't be cast to a number.

Also, depending on the data, you might still end up with errors. In some cases, SQL might choose to evaluate the CAST before the WHERE clause.

For SQL 2012 or higher, TRY_PARSE[^] would be a better option:
SELECT
    name_costomer1,
    MAX(TRY_PARSE(number_qamema As decimal(15, 4))) As NB_QE
FROM
    NB_QE
GROUP BY
    name_costomer1
;
an0ther1 23-Jan-18 16:04pm    
Thanks for the info Richard, much appreciated.

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