Solved

Convert varchar into int

Posted on 2013-06-11
8
896 Views
Last Modified: 2013-06-11
Trying to convert data in varchar format into an integer so I can sum the values.
However, some of the data contains strings like '-11.73' or '$1.23' or '$1,500'
I am getting the error below with this code. Please help. Thanks.

Msg 245, Level 16, State 1, Line 3
Conversion failed when converting the varchar value '-11,73' to data type int.


SELECT
'YTD' as "Totals"
,'System' as "Type"
,count(distinct c.ch_trade_error_id) as "Total Trade Errors"
,sum(case when cast(replace(c.amount,'.',',') as int) *-1 < 0 then cast(replace(c.amount,'.',',') as int) else 0 end) as Gain


FROM
CHAMP.V_TRADE_ERRORS c


WHERE
c.ORIGINATION_DATE between '2013-01-01' and '2013-05-31'
and c.REP_NAME is null
0
Comment
Question by:saved4use
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 23

Accepted Solution

by:
Racim BOUDJAKDJI earned 500 total points
ID: 39239569
replace

sum(case when cast(replace(c.amount,'.',',') as int) *-1 < 0 then cast(replace(c.amount,'.',',') as int) else 0 end)

Open in new window


by

sum(case when cast(replace(floor(c.amount),'.',',') as int) *-1 < 0 then cast(replace(floor(c.amount),'.',',') as int) else 0 end)

Open in new window

0
 

Author Comment

by:saved4use
ID: 39239590
Used the above and got the error below:

Msg 8114, Level 16, State 5, Line 3
Error converting data type varchar to float.
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 39239614
You perhaps have some value stored in varchar that are non numeric value.  You need to get rid of these first by identifying them.

Run below to do that...

select top 1000 c.amount
from CHAMP.V_TRADE_ERRORS
where isnumeric(c.amount) <> 1

Open in new window

0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:saved4use
ID: 39239633
@Racimo You are right! How do I get rid of them? *I definitely need to sum those amounts in parenthesis and dollar sign. Here they are

amount
--------------------
$487-50
(.12)
(.72)
(.84)
(0.03)
(0.27)
(0.73)
(1)
(1.30)
(10.00)
(10.09)
(102.00)
(1035.31)
(104.91)
(11.58)
(12.00)
(120.65)
(1204.10)
(130)
(1400.00)
(15.00)
(150.00)
(156)
(16.00)
(178.52)
(1947.17)
(198)
(2.00)
(2.08)
(20.00)
(200)
(21.35)
(212.84)
(214)
(219)
(220)
(226.73)
(230)
(24.00)
(250)
(26.00)
(260.84)
(278.17)
(28.50)
(28.70)
(3.24)
(32.57)
(324.97)
(33.00)
(33.44)
(330.00)
(35)
(3557.71)
(37.92)
(375.18)
(398.26)
(43.50)
(436.65)
(44.00)
(45.00)
(450)
(5.12)
(5.60)
(5.69)
(532.66)
(54.00)
(54.07)
(57.60)
(6.00)
(679.90)
(7.15)
(70.40)
(734.85)
(750)
(80.00)
(81.71)
(813.00)
(84.76)
(99.19)
*
**
***
****
*****
******
*******
*****************
N/A
NOT KNOWN

(89 row(s) affected)
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 39239649
How do I get rid of them?
1> Well for one, it seems some values are stored in parenthesis.  You simply need to get rid of these parenthesis by running something like

update CHAMP.V_TRADE_ERRORS
set c.amount = substring(rtrim(c.amount), 2, len(rtrim(c.amount)) -2)
where isnumeric(c.amount) <> 1 and left(rtrim(c.amount), 1) = '('
and right(rtrim(c.amount), 1) = ')'

Open in new window


once you get this simply delete values that can not be used at all by running

delete
from CHAMP.V_TRADE_ERRORS
where isnumeric(c.amount) <> 1

Open in new window

0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 39239651
Yes and please test this before running into production.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39239866
use of parentheses can be used to indicate negative

just as an observation here, do values within parentheses indicate a NEGATIVE number?
if so, removing the parentheses without also negating the value would produce an incorrect total.

also, for the first listed value above: $487-50

is this $437 ($487 - $50) or $487.50?

{+edit}
my guess is that this data is coming from Excel
N/A indicates some sort of logical error
and ***** may indicate another form of logical error

{+edit 2}
and why are you converting decimals to integer anyway?
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39239890
&&
TRUE_AMOUNT_SUM  SUM_AMOUNT_TO_INT  INT_AMOUNT_SUM
-19572.69                     -19573                               -19576

taking those numbers in parentheses, pasting into Excel (automatically treated as negatives), then using in sql server and then summing with this:
select
  sum(amount) as true_amount_sum
, cast(sum(amount) as int) as sum_amount_to_int
, sum(cast(amount as int)) as int_amount_sum
from table1
http://sqlfiddle.com/#!3/78de3/3
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question