2017-06-05 5 views
1

データベーステーブルにこのカラムがあり、その文字列があります。私はこのテーブルを作っていないし、データタイプを変更することはできません。SQLをカラムを浮動小数点に変換する

しかし、浮動小数点数と比較できるように、浮動小数点数をwhere句の浮動小数点数に変換する必要があります。私が間違っているのは何

An error occurred while reading from the store provider's data reader. See the inner exception for details 

:私はこのエラーを得たとして

sql += " AND CAST (ec.bedrooms as float) <= " + id.beds.Max().Split('-')[1] + " AND CAST (ec.bedrooms as float) >= " + id.beds.Min().Split('-')[0]; 

それdidntの仕事:

私はfloatとしてキャスト私のコラムを試してみましたか?ここで

は、そのセクション

AND CAST (ec.bedrooms as float) <= 3 AND CAST (ec.bedrooms as float) >= 2 

"InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Error converting data type nvarchar to float.","ExceptionType":"System.Data.SqlClient.SqlException","StackTrace":" at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)\r\n at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)\r\n at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)\r\n at System.Data.SqlClient.SqlDataReader.TryHasMoreRows(Boolean& moreRows)\r\n at System.Data.SqlClient.SqlDataReader.TryReadInternal(Boolean setTimeout, Boolean& more)\r\n at System.Data.SqlClient.SqlDataReader.Read()\r\n at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.StoreRead()"}} 
+3

をご提示ください*実際の*生成されたクエリ – Siyual

+0

スタックトレース – hardkoded

+4

SQLクエリを構築するには、実際にパラメータを使用する必要があります。 –

答えて

0

のために生成されたもので、数値以外の値をしているあなたのec.bedroomsように見える...あなたは以下のように確認することができます。

sql += " AND CAST (iif(isnumeric(ec.bedrooms)>0,ec.bedrooms,null) as float) <= " + id.beds.Max().Split('-')[1] + " AND 
CAST (iif(isnumeric(ec.bedrooms)>0,ec.bedrooms,null) as float) >= " + id.beds.Min().Split('-')[0]; 
関連する問題