Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code is ok, but if have values from 0 to 999 its ok
 
Exmpl two values 500.00 and 80.00 result is 580.00
That is ok
 
Exmp if have two values   1,000.00 and 100.00 result is 100.00
 
Some help?


What I have tried:

select kupac as 'Kupac',
SUM(TRY_CONVERT(Decimal(10, 2), ISNULL(NULLIF(iznos_sa_pdv, ''), '0'))) AS 'UKUPNO'   
from(select kupac, iznos_sa_pdv from mp_faktura_lista union all select kupac, iznos_sa_pdv from mp_racun_lista) as alltables  
group by kupac  
Posted
Updated 29-Dec-18 18:37pm
Comments
Richard MacCutchan 29-Dec-18 4:03am    
Why are you storing numbers as character types?
Goran Bibic 29-Dec-18 4:06am    
I try to solve the problem, men work before me in this company
Richard MacCutchan 29-Dec-18 4:12am    
The way to solve it is to use proper numeric types. The time you spend trying to work round someone else's stupidity is usually time wasted. Bite the bullet now and fix the database.
Goran Bibic 29-Dec-18 4:19am    
I know. Have half finished solution. Must repair. To add new column with type numeric in sql table? And visual studio...etc...Right?

If you can't change the DB design for a reason, you need just to eliminate ',' from string represantation of numeric
SQL
SUM(TRY_CONVERT(Decimal(10, 2), ISNULL(NULLIF(REPLACE(iznos_sa_pdv,',','' )''), '0')))

Currently TRY_CONVERT() returns NULL for non-covertible string and SUM() ignores NULLs so
values kind of '1,500' are ignored.
 
Share this answer
 
v2
I'd start by checking your DB data itself - we have no access to it, but I'd suspect that iznos_sa_pdv contains some empty strings.

Try it in SSMS, and when it gives the "wrong" result, start breaking it up a little to find out exactly what it is processing as data.
 
Share this answer
 
Comments
Goran Bibic 29-Dec-18 3:14am    
Not understand my question.
Value is null sql write 0 that is ok
Value is 500.00 sql write 500.00 its ok

Problem is when have values bigger then 999 with separator , 1,456.00

When is sum in sql 500.00+1,456.00 result is 500.00
Separator , is problem

Some help?
OriginalGriff 29-Dec-18 3:24am    
Show us the whole data rows that generate the problem, and the version that doesn't.
We can't work with "my data is ok" which is what you are in effect saying...
Goran Bibic 29-Dec-18 3:27am    
Thank you. Regards
From what I gather, you store numbers in a string column in the database. In the SUM operation you end up in problems because of the thousand separators.

If this is the problem, I believe that the only reasonable correction would be to change the data type for the column and store the numbers as numbers, decimal perhaps.
 
Share this answer
 
Comments
OriginalGriff 29-Dec-18 3:37am    
You are probably right - but without his actual data it's hard to tell ... :sigh:
Goran Bibic 29-Dec-18 3:49am    
datatype is varchar (255)
Wendelius 29-Dec-18 4:16am    
That's what I suspected. So I would suggest using a numeric data type for the column since you store numbers in it. After that you wouldn't need to do conversions and summing items would be easy to do, not to mention performance gain.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900