Solved

Error converting data type varchar to numeric. (CASTing)

Posted on 2009-07-03
9
1,044 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
9 Comments
 
LVL 41

Accepted Solution

by:
pcelba earned 500 total points
Comment Utility
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 41

Expert Comment

by:pcelba
Comment Utility
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
Comment Utility
I tried price_min.ToString("f5"). It creates good String (5 digit places after dot), but problem is still...
0
 
LVL 9

Expert Comment

by:Rahul Goel
Comment Utility
price_min.ToString("###.00")
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 2

Expert Comment

by:nkiggs
Comment Utility
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
Comment Utility
The problem is not about price_min String formatting
0
 

Author Comment

by:damixa
Comment Utility
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
Comment Utility
Found mistake....
just changed "," to "." in string
0
 
LVL 41

Expert Comment

by:pcelba
Comment Utility
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now