Solved

Convert varchar into int

Posted on 2013-06-11
8
875 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

856 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