Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 conversion failed when converting the nvarchar val

Author  Topic 

learning_grsql
Posting Yak Master

230 Posts

Posted - 2012-08-18 : 09:21:37
hi,

I have a column(quantity) with data type varchar(50) but values are like - 0, 1, 2, 3, 4

When I try to find sum for that column as below, i get error "Conversion failed when converting the nvarchar value 'quantity' to data type int."

date, sum(cast(quantity as int)) as quantity from table1

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-08-18 : 09:24:30
quote:
I have a column(quantity) with data type varchar(50) but values are like - 0, 1, 2, 3, 4

that is one value or several rows of value ?



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

learning_grsql
Posting Yak Master

230 Posts

Posted - 2012-08-18 : 09:45:12
SEVERAL ROWS OF VALUE...IN ONE ROW IT IS JUST '0' SECOND ROW '1', THIRD ROW '7'...AND SO ON..
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-18 : 10:20:35
Do you actually have a row that has the value "quantity" in the table? You can find using:
SELECT quantity from YourTable where quantity = 'quantity';
If you do remove that row. If you inserted the data from a text file and if it had a column header which was not identified as such during the import process, this can happen.

Another thing you can do is to run this query:
SELECT * FROM YourTable WHERE ISNUMERIC(quantity) = 0
That will tell you if there are any rows that are not convertible to one of the numeric types. But, that test is not perfect - ISNUMERIC will return 1 if the value is can be converted to ANY numeric type, even if it cannot be converted to INT.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2012-08-18 : 10:20:52
then you must have value that is not numeric in there

use isnumeric() to validate your data

select *
from yourtable
where isnumeric(quantity) <> 1



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

learning_grsql
Posting Yak Master

230 Posts

Posted - 2012-08-18 : 10:59:32
quote:
Originally posted by sunitabeck

Do you actually have a row that has the value "quantity" in the table? You can find using:
SELECT quantity from YourTable where quantity = 'quantity';
If you do remove that row. If you inserted the data from a text file and if it had a column header which was not identified as such during the import process, this can happen.

Another thing you can do is to run this query:
SELECT * FROM YourTable WHERE ISNUMERIC(quantity) = 0
That will tell you if there are any rows that are not convertible to one of the numeric types. But, that test is not perfect - ISNUMERIC will return 1 if the value is can be converted to ANY numeric type, even if it cannot be converted to INT.


Again thanks a lot Sunita and Khtan.
The problem was I had a row with value 'quantity" in column quantity. I just deleted the row after seeing your reply. It works now.
Go to Top of Page
   

- Advertisement -