SQL Clone
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
Old Hand
Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)Old Hand (353 reputation)

Group: General Forum Members
Points: 353 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
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43027 Visits: 20015
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
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5480 Visits: 2768
Chris solution will work Smile

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

Group: General Forum Members
Points: 10616 Visits: 5157
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,
My blog: www.igormicev.com
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43027 Visits: 20015
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
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10616 Visits: 5157
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,
My blog: www.igormicev.com
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43027 Visits: 20015
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
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10616 Visits: 5157
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,
My blog: www.igormicev.com
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43027 Visits: 20015
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
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10616 Visits: 5157
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,
My blog: www.igormicev.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