Sunday, July 5, 2009

SQL Error: Disallowed implicit conversion from data type varchar to data type money

The Error Disallowed implicit conversion from data type varchar to data type money usually occurs when try to assign String Value to Money Data Type of Sql Server.


Possible Reasons:


1. Assign String(Varchar) Value to Sql Server Column value which is of Money data type.

2. Especially in Insert and Update Queries

Assume Account Table in Sql Server contains a Field Amount which is of Type "Money".


Example1:

Insert Query

strSQL = "Insert INTO AccountTable(Amount) Values (" & txtamount.text & ")"

MyConn.Execute(strSQL)


Example2:

Update Query

strSQL = "UPDATE AccountTable SET Amount = " & txtamount.text

MyConn.Execute(strSQL)



Solutions:


Need to Convert the Value of Varchar Type or String Type to Money.


How to convert Varchar to Money Type?

Need to Use CONVERT Function and Single Quotes.

Modified Query:

strSQL = "Insert INTO AccountTable(Amount) Values (CONVERT(money,'" & txtamount.text & "'))"

strSQL = "UPDATE AccountTable SET Amount = (CONVERT(money,'" & txtamount.text & "'))"


Important:

(Note the single-quote just after the word money, and the single-quote and end paren just prior to the final comma.)

No comments:

Post a Comment