Solved

Convert varchar into int

Posted on 2013-06-11
8
862 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
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
 

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 48

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 48

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Breakdown by month across past 3 years 6 40
SQL Login 17 38
SQL Query stumper 3 36
How do query sql to display current month date ? 15 9
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 …
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

914 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

18 Experts available now in Live!

Get 1:1 Help Now