We help IT Professionals succeed at work.

SQL Select Casting Question

hojohappy
hojohappy asked
on
269 Views
Last Modified: 2014-08-24
I have a file that I am trying to query with a column that contains formatted number values like

TotalRevenue
----------------
 99,200
500,000.00
4.00
1,200.00

I am trying to get all the rows that have total revenue > 100000.   When I run the query below I get an error "error converting nvarchar to int".   How can I fix the query below?

SELECT *
FROM [X.csv]
INNER JOIN [B.csv] ON [X.csv].[Product Code] = [B.csv].[Product_Code]
WHERE
([X.csv].[TotalRevenue] > 100000)
Comment
Watch Question

Scott PletcherSenior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
First try:

WHERE
 ([X.csv].[TotalRevenue] > 100000.0)
Senior Developer
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Shaun KlineLead Software Engineer
CERTIFIED EXPERT

Commented:
You need to change your WHERE clause to:
WHERE CAST(RTRIM(LTRIM([X.csv].[TotalRevenue])) AS decimal(9.2)) > 100000.00

Open in new window


There are two issues occurring. SQL is trying to cast the field into an integer because of the compared value, and your field appears to have leading and trailing spaces. You should also include the ISNUMERIC(<field>) = 1 in your WHERE clause:
WHERE CAST(RTRIM(LTRIM([X.csv].[TotalRevenue])) AS decimal(9.2)) > 100000.00
AND ISNUMERIC([X.csv].[TotalRevenue]) = 1

Open in new window

Jim HornSQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015

Commented:
>500,000.00
Part of the deal-i-o here is that numeric data types store only the number, and not a thousands-separating-comma, which makes the value not convertable to numeric.  You'll have to lose the comma to manipulate it as a number.

Something like (in addition to the above comments)
-- This throws an 'Error converting data type varchar to numeric' error. 
SELECT CAST('123,456.78' as numeric)

-- This works
SELECT CAST(REPLACE('123,456.78', ',', '') as numeric(8,2))

Open in new window

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.