Solved

Convert varchar into int

Posted on 2013-06-11
8
858 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
  • 4
  • 2
  • 2
8 Comments
 
LVL 23

Accepted Solution

by:
Racim BOUDJAKDJI earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:saved4use
Comment Utility
@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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
Comment Utility
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
Comment Utility
Yes and please test this before running into production.
0
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
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 48

Expert Comment

by:PortletPaul
Comment Utility
&&
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now