?
Solved

Convert varchar into int

Posted on 2013-06-11
8
Medium Priority
?
937 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

862 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