Solved

Error converting data type varchar to numeric. (CASTing)

Posted on 2009-07-03
9
1,047 Views
Last Modified: 2013-12-17
Hi,

I got problem with SQL statement (SELECT string) generated in C# code.
Field products.product_price in DB type is decimal(18,2).
I want to pass double variable included in SELECT string (below code), but given error "Error converting data type varchar to numeric."
Tried to execute this  statement with same parameter (for example '100.555555' as price_min) in SQL editor in MS DBMS and it works.

Thanks


Double price_min = Convert.ToDouble(filtersPriceMinBox.Text) * Convert.ToDouble(curr_in_euros_rate);
 
adapterSelectStmt += " and (prod.product_price > (CAST ( '" + price_min + "' AS decimal(18, 2))))";

Open in new window

0
Comment
Question by:damixa
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 42

Accepted Solution

by:
pcelba earned 500 total points
ID: 24771388
The most probably reason is the conversion of your "Double price_min" to varchar value when you are building the condition: ... (CAST ( '" + price_min + "' AS decimal ...

You have to use some explicit conversion because concatenating your double value of price_min to the text string without any conversion will result to scientific notation:

CAST('100.55555e00' AS decimal(18,2))

and this is not properly recognized by SQL engine.
0
 
LVL 42

Expert Comment

by:pcelba
ID: 24771427
The proper conversion seems to be   price_min.ToString("f5")  (Hope you know C# better than me and correct it.)
0
 

Author Comment

by:damixa
ID: 24771475
I tried price_min.ToString("f5"). It creates good String (5 digit places after dot), but problem is still...
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
LVL 9

Expert Comment

by:Rahul Goel ITIL
ID: 24771498
price_min.ToString("###.00")
0
 
LVL 2

Expert Comment

by:nkiggs
ID: 24771500
Try truncating any spaces to ensure there are no spaces which may not be recognised as valid.
To parse simply use vartype.Parse(input string);
in this case try:
Decimal.Parse(trim(price_min))
0
 

Author Comment

by:damixa
ID: 24771519
The problem is not about price_min String formatting
0
 

Author Comment

by:damixa
ID: 24771610
Tried with parsing:                

String str = price_min.ToString("f4");
Decimal.Parse(str.Trim());

adapterSelectStmt += " and (prod.product_price > (CAST ( '" + str + "' AS decimal(18, 2) ) ) )";

but still, same error..

On the other hand, this works:

adapterSelectStmt += " and (prod.product_price > (CAST ( '" + "1000.5555" + "' AS decimal(18, 2) ) ) )";

0
 

Author Comment

by:damixa
ID: 24771801
Found mistake....
just changed "," to "." in string
0
 
LVL 42

Expert Comment

by:pcelba
ID: 24771838
So, the problem was in conversion of double to string as I mentioned in the first answer (the scientific notation was just one possible reason)... The comma raised from your regional settings probably and without this information is almost impossible to answer correctly.

The best solution is to display the whole SQL command and test it in SQL Management studio.
0

Featured Post

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question