ADO SQL类型转换浮点到字符串(Excel) [英] ADO SQL type cast Float to String (Excel)

查看:602
本文介绍了ADO SQL类型转换浮点到字符串(Excel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用ADO中的CAST将Float值转换为String?

How do I use the CAST in ADO to convert Float values to String?

我尝试过

SELECT CAST([Field] AS VARCHAR(20)) FROM ...

SELECT CAST([Field] AS STRING) FROM ...

并总是得到一个OLE异常(未知错误)。

and always get an OLE Exception (Unknown error).

表列包含混合数字(右对齐)和字母数字(左对齐)值。如果只有字母数字值,则ADO查询字段类型为String。

The table column contains mixed numeric (right justified) and alphanumeric (left justified) values. If there are only alphanumeric values, the ADO query field type is String.

我正在使用Delphi 2009 ADO和Excel 2010。

I am using Delphi 2009 ADO and Excel 2010.

推荐答案

CAST是SQL Server的表达式。在delphi中使用 SELECT Field FROM ...

CAST is SQL-Server expression. use SELECT Field FROM...

ADOQuery.FieldByName('Field ').AsString

in delphi: ADOQuery.FieldByName('Field').AsString

你不能通过SQL语句转换。

you cannot cast it via SQL statement.

使用混合数据类型

阅读来自MSDN 关于混合数据类型的注意事项):


ADO必须猜测Excel
工作表或范围中每列的数据类型。 (这不受Excel单元格格式
设置的影响。)如果数值与
混合在同一列中的文本值,则可能会出现严重问题。 Jet和ODBC
提供程序都返回多数类型的数据,但为少数数据类型返回NULL(空)
值。如果两个类型在列中同样混合
,则提供者将选择文本中的数字。

ADO must guess at the data type for each column in your Excel worksheet or range. (This is not affected by Excel cell formatting settings.) A serious problem can arise if you have numeric values mixed with text values in the same column. Both the Jet and the ODBC Provider return the data of the majority type, but return NULL (empty) values for the minority data type. If the two types are equally mixed in the column, the provider chooses numeric over text.

您将需要添加连接字符串的扩展属性部分中的 IMEX = 1
持续字段将为TWideStringField。

you will need to add IMEX=1 in the Extended Properties section of the connection string. the persistent field will be of TWideStringField.

连接字符串应如下所示:

The connection string should look something like this:

Provider = Microsoft.Jet.OLEDB.4.0; Password =; User ID = Admin; Data Source = C:\MyFile.xls; Mode = Share Deny None; Extended Properties =Excel 8.0 ; IMEX = 1; ...

这篇关于ADO SQL类型转换浮点到字符串(Excel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆