Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 simple query

Author  Topic 

rizo
Starting Member

16 Posts

Posted - 2012-03-12 : 08:10:29
hi guys,

I have a nvarchar field populated with text and numbers.
whats is the most simple way to sum all numbers in a nvarchar datatype?

In access the statement would be like this
Sum(IIf(IsNumeric(datacolumn),datacolumn,0)))

rizo

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-12 : 08:15:03
[code]SUM( CASE WHEN ISNUMERIC(datacolumn) = 1 THEN CAST(datacolumn AS FLOAT) ELSE 0 END )
[/code]
Go to Top of Page

rizo
Starting Member

16 Posts

Posted - 2012-03-12 : 08:33:26
this throws error
Error converting data type nvarchar to float.



rizo
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-12 : 08:35:37
Sorry, my bad. See correction in red above.
Go to Top of Page

rizo
Starting Member

16 Posts

Posted - 2012-03-12 : 08:44:06
it's not that because I spotted and fixed that.
I still get the same error
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-12 : 09:22:52
Do you have any rows with dashes? If so that can happen. If converting to INT would suffice, change the FLOAT to INT. This fails:
DECLARE @x NVARCHAR(32);
SET @x = '-';
SELECT CASE WHEN ISNUMERIC(@x) = 1 THEN CAST(@x AS FLOAT) ELSE 0 END
This succeeds
DECLARE @x NVARCHAR(32);
SET @x = '-';
SELECT CASE WHEN ISNUMERIC(@x) = 1 THEN CAST(@x AS INT) ELSE 0 END
Go to Top of Page

rizo
Starting Member

16 Posts

Posted - 2012-03-12 : 10:26:29
I tries and I get teh same error.
Conversion failed when converting the nvarchar value '344.82' to data type int. - when converting to INT
Error converting data type nvarchar to float. - when converting to FLOAT

I'm with the idea that I have to remove the dashes and and commas?


rizo
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-12 : 11:05:41
You can use the following code segment to replace dashes and commas. I suspect negative numbers may be shown using brackets, for example, -234 as (234). This takes care of that too:
REPLACE
(
CASE
WHEN datacolumn = '-' THEN '0'
WHEN datacolumn LIKE '(%)' THEN REPLACE(REPLACE(datacolumn,'(','-'),')','')
END,
',',''
)
So then you will need to replace each occurrence of datacolumn in your expression with the above.
SUM( CASE WHEN ISNUMERIC(
REPLACE
(
CASE
WHEN datacolumn = '-' THEN '0'
WHEN datacolumn LIKE '(%)' THEN REPLACE(REPLACE(datacolumn,'(','-'),')','')
END,
',',''
)

) = 1 THEN CAST(
REPLACE
(
CASE
WHEN datacolumn = '-' THEN '0'
WHEN datacolumn LIKE '(%)' THEN REPLACE(REPLACE(datacolumn,'(','-'),')','')
END,
',',''
)
AS FLOAT) ELSE 0 END )
It gets ugly very fast, doesn't it. Many SQL experts would advise you to use the correct data type for the column, which then would force you insert only valid data. That would help in all sorts of things, including querying, avoiding costly and time-consuming mistakes and errors in the future etc.
Go to Top of Page

rizo
Starting Member

16 Posts

Posted - 2012-03-12 : 11:26:20
thanks for the reply.
This is proving more than a challenge I thought it would be.
I'll have to find another way around it.
thank you very much for your help


rizo
Go to Top of Page
   

- Advertisement -