SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query throwing error when converting to Numeric


Query throwing error when converting to Numeric

Author
Message
vijay.singh 14112
vijay.singh 14112
Mr or Mrs. 500
Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)

Group: General Forum Members
Points: 569 Visits: 185
Hi Experts,

Please help me in this.

I have a table where i am storing numeric value in Varchar column.
when i am trying to convert it to numeric its throwing an error. Please find the error and query below.

Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.


SELECT * FROM MyDB..MyTable where BatchFileId=5 AND CAST(LineItemValue AS NUMERIC(28,5))=1873.76

if i convert it to float and run the below query, then its working fine.


SELECT * FROM MyDB..MyTable
where BatchFileId=5 AND CAST(LineItemValue AS FLOAT)=1873.76

Please help me to fix this problem.

Thanks in Advance.

Regards,

Vijay
Phil Parkin
Phil Parkin
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18271 Visits: 20409
Can you provide us with DDL & sample data that will allow us to replicate this?


Help us to help you. For better, quicker and more-focused answers to your questions, consider following the advice in this link.

If the answer to your question can be found with a brief Google search, please perform the search yourself, rather than expecting one of the SSC members to do it for you.

Please surround any code or links you post with the appropriate IFCode formatting tags. It helps readability a lot.
vijay.singh 14112
vijay.singh 14112
Mr or Mrs. 500
Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)

Group: General Forum Members
Points: 569 Visits: 185
Hi,

Please find the code below.

CREATE TABLE MyTable
(
BatchFileID INT IDENTITY,
LineItemValue NVARCHAR(256)
)

INSERT MyTable
SELECT 'EFC'
UNION
SELECT '3456.5070'
UNION
SELECT '253434.5670'
UNION
SELECT '345.5070'
UNION
SELECT '1234.5670'

select * from MyTable
WHERE
--CAST(LineItemValue AS NUMERIC(28,5))=1234.5670 AND
LineItemValue NOT LIKE '%E%'
AND ISNUMERIC(LineItemValue)=1

select * from MyTable
WHERE
CAST(LineItemValue AS NUMERIC(28,5))=1234.5670 AND
LineItemValue NOT LIKE '%E%'
AND ISNUMERIC(LineItemValue)=1

DROP TABLE MyTable

I am unable to reproduce the scenario for float. once i reproduce i will post the code back.

But above code for converting nvarchar to numeric should work. Just wanted to know why it is not working.

Thanks,

Vijay
laurie-789651
laurie-789651
SSChasing Mays
SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)SSChasing Mays (616 reputation)

Group: General Forum Members
Points: 616 Visits: 1272
Hi Vijay

I've tried a few options on this one, and the only one that works is to create a temp table with the Numeric values (Excluding any alpha), then select from the temp table to get the required values.

The execution plan is always converting the query to a single table scan & outputting LineItemValue, so that's why it fails.

Why it's doing that & how to stop it, I don't know.

Examples: (which have the same problem)


SELECT BatchFileId, Num1
FROM
(
SELECT BatchFileId,
CAST(LineItemValue AS NUMERIC(28,5)) as Num1
FROM MyTable
WHERE LineItemValue NOT LIKE '%E%'
) X
WHERE Num1=1234.5670


SELECT T.BatchFileId,
X.Num1
FROM MyTable T
CROSS APPLY (SELECT CAST(T.LineItemValue AS NUMERIC(28,5)) as Num1
WHERE T.LineItemValue NOT LIKE '%E%') X
WHERE ISNUMERIC(X.Num1)=1 AND X.Num1=1234.5670




Maybe someone else knows what's happening & how to fix it?Ermm
vijay.singh 14112
vijay.singh 14112
Mr or Mrs. 500
Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)Mr or Mrs. 500 (569 reputation)

Group: General Forum Members
Points: 569 Visits: 185
Thanks laurie:-).

Even i tried the first Query and its working fine. But just wanted to know why its is not working in Usual way.

Regards,

Vijay
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16105 Visits: 19534
ISNUMERIC isn't very useful, see this article.

(CROSS/OUTER) APPLY can be a handy and efficient alternative:

SELECT 
m.BatchFileId,
x.Num1
FROM MyTable m
CROSS APPLY (
SELECT Num1 = CAST(
CASE WHEN m.LineItemValue NOT LIKE '%E%' THEN m.LineItemValue ELSE NULL END
AS NUMERIC(28,5))
) x
WHERE x.Num1 IS NOT NULL



“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
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