Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to sum in SQL removing varchar


How to sum in SQL removing varchar

Author
Message
niladri.primalink
niladri.primalink
SSC-Enthusiastic
SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)

Group: General Forum Members
Points: 117 Visits: 226
Hi All,

I am trying to sum of amount (data type varchar) column but I am getting error

error converting data type varchar to float.

when I checked the data very carefully I found that there is an amount like C000214534. But when I am removing that varchar amount I am not getting error

select sum(CONVERT(float, ft.amount)) FROM filetransaction ft WHERE ft.fileid <>332 this query gives me the result.

is there any way in sql so that it can sum removing varchar? I do not want to update as everyday the data is inserted from the client's end.

How can I sum those data except the data where varchar type.

Please help!!
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8923 Visits: 19005
SELECT 
   SUM(x.FloatAmount)
FROM filetransaction ft
CROSS APPLY (
   SELECT FloatAmount = CASE
      WHEN ISNUMERIC(ft.amount) = 1 THEN CAST(ft.amount AS FLOAT)
      ELSE 0 END
) x
WHERE 1 = 1



NOTE: ISNUMERIC has limitations. Here's a good reference with workarounds.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2383 Visits: 2763
Chris solution will work Smile

_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Igor Micev
Igor Micev
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4158 Visits: 4843
Hi

SELECT sum(CONVERT(float, ft.amount)) 
FROM filetransaction ft
WHERE ft.fileid <>332 and ISNUMERIC(ft.amount)=1



Check this also http://www.sqlservercentral.com/articles/IsNumeric/71512/

Regards
IgorMi


Igor Micev,SQL Server developer at Seavuswww.seavus.com
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8923 Visits: 19005
IgorMi (5/3/2013)
Hi

SELECT sum(CONVERT(float, ft.amount)) 
FROM filetransaction ft
WHERE ft.fileid <>332 and ISNUMERIC(ft.amount)=1



Check this also http://www.sqlservercentral.com/articles/IsNumeric/71512/

Regards
IgorMi


Which of those two filters will SQL Server apply first?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Igor Micev
Igor Micev
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4158 Visits: 4843
ChrisM@Work (5/3/2013)
IgorMi (5/3/2013)
Hi

SELECT sum(CONVERT(float, ft.amount)) 
FROM filetransaction ft
WHERE ft.fileid <>332 and ISNUMERIC(ft.amount)=1



Check this also http://www.sqlservercentral.com/articles/IsNumeric/71512/

Regards
IgorMi


Which of those two filters will SQL Server apply first?


Filters in queries having WHERE clause connected by AND operators are evaluated from left to right in the order they are written.


Igor Micev,SQL Server developer at Seavuswww.seavus.com
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8923 Visits: 19005
IgorMi (5/3/2013)
ChrisM@Work (5/3/2013)
IgorMi (5/3/2013)
Hi

SELECT sum(CONVERT(float, ft.amount)) 
FROM filetransaction ft
WHERE ft.fileid <>332 and ISNUMERIC(ft.amount)=1



Check this also http://www.sqlservercentral.com/articles/IsNumeric/71512/

Regards
IgorMi


Which of those two filters will SQL Server apply first?


Queries having WHERE clause connected by AND operators are evaluated from left to right in the order they are written.


What is the source for this statement? It's completely wrong.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Igor Micev
Igor Micev
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4158 Visits: 4843
ChrisM@Work (5/3/2013)
IgorMi (5/3/2013)
ChrisM@Work (5/3/2013)
IgorMi (5/3/2013)
Hi

SELECT sum(CONVERT(float, ft.amount)) 
FROM filetransaction ft
WHERE ft.fileid <>332 and ISNUMERIC(ft.amount)=1



Check this also http://www.sqlservercentral.com/articles/IsNumeric/71512/

Regards
IgorMi


Which of those two filters will SQL Server apply first?


Queries having WHERE clause connected by AND operators are evaluated from left to right in the order they are written.


What is the source for this statement? It's completely wrong.


Aham
I now see. It should be:

SELECT sum(CONVERT(float, ft.amount)) 
FROM filetransaction ft
WHERE ISNUMERIC(ft.amount)=1



I copied ft.fileid <>332 mistakenly.


Igor Micev,SQL Server developer at Seavuswww.seavus.com
ChrisM@Work
ChrisM@Work
SSCrazy Eights
SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)SSCrazy Eights (8.9K reputation)

Group: General Forum Members
Points: 8923 Visits: 19005
IgorMi (5/3/2013)
ChrisM@Work (5/3/2013)
IgorMi (5/3/2013)
ChrisM@Work (5/3/2013)
IgorMi (5/3/2013)
Hi

SELECT sum(CONVERT(float, ft.amount)) 
FROM filetransaction ft
WHERE ft.fileid <>332 and ISNUMERIC(ft.amount)=1



Check this also http://www.sqlservercentral.com/articles/IsNumeric/71512/

Regards
IgorMi


Which of those two filters will SQL Server apply first?


Queries having WHERE clause connected by AND operators are evaluated from left to right in the order they are written.


What is the source for this statement? It's completely wrong.


Aham
I now see. It should be:

SELECT sum(CONVERT(float, ft.amount)) 
FROM filetransaction ft
WHERE ISNUMERIC(ft.amount)=1



I copied ft.fileid <>332 mistakenly.


Do you want to include the rows where ft.amount cannot be converted to a numeric datatype?

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Igor Micev
Igor Micev
SSCarpal Tunnel
SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)SSCarpal Tunnel (4.2K reputation)

Group: General Forum Members
Points: 4158 Visits: 4843
ChrisM@Work (5/3/2013)
IgorMi (5/3/2013)
ChrisM@Work (5/3/2013)
IgorMi (5/3/2013)
ChrisM@Work (5/3/2013)
IgorMi (5/3/2013)
Hi

SELECT sum(CONVERT(float, ft.amount)) 
FROM filetransaction ft
WHERE ft.fileid <>332 and ISNUMERIC(ft.amount)=1



Check this also http://www.sqlservercentral.com/articles/IsNumeric/71512/

Regards
IgorMi


Which of those two filters will SQL Server apply first?


Queries having WHERE clause connected by AND operators are evaluated from left to right in the order they are written.


What is the source for this statement? It's completely wrong.


Aham
I now see. It should be:

SELECT sum(CONVERT(float, ft.amount)) 
FROM filetransaction ft
WHERE ISNUMERIC(ft.amount)=1



I copied ft.fileid <>332 mistakenly.


Do you want to include the rows where ft.amount cannot be converted to a numeric datatype?


No, but I posted "Check this also http://www.sqlservercentral.com/articles/IsNumeric/71512/"


Igor Micev,SQL Server developer at Seavuswww.seavus.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search