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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 simple query
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

rizo
Starting Member

United Kingdom
16 Posts

Posted - 03/12/2012 :  08:10:29  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 03/12/2012 :  08:15:03  Show Profile  Reply with Quote
SUM( CASE WHEN ISNUMERIC(datacolumn) = 1 THEN CAST(datacolumn AS FLOAT) ELSE 0 END )

Edited by - sunitabeck on 03/12/2012 08:35:18
Go to Top of Page

rizo
Starting Member

United Kingdom
16 Posts

Posted - 03/12/2012 :  08:33:26  Show Profile  Reply with Quote
this throws error
Error converting data type nvarchar to float.



rizo
Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

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

rizo
Starting Member

United Kingdom
16 Posts

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

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 03/12/2012 :  09:22:52  Show Profile  Reply with Quote
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

Edited by - sunitabeck on 03/12/2012 09:23:38
Go to Top of Page

rizo
Starting Member

United Kingdom
16 Posts

Posted - 03/12/2012 :  10:26:29  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 03/12/2012 :  11:05:41  Show Profile  Reply with Quote
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

United Kingdom
16 Posts

Posted - 03/12/2012 :  11:26:20  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000