vba Excel 和 SQL:如何在 Select 语句中将字符串转换为双精度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4615869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:29:06  来源:igfitidea点击:

Excel & SQL: How to convert string to double in Select statement

sqlvbavbscriptado

提问by vmg

I have a sheet in Excel. One column in it ("ID") contains strings with values: 1.01, 1.02, 1.03, 2.01, 3.01, 3.04 etc.

我有一张 Excel 表格。其中的一列(“ID”)包含具有以下值的字符串:1.01、1.02、1.03、2.01、3.01、3.04 等。

I want to get all rows in that sheet, where "ID" is >= some value using SQL statement and ADO connection (from VBScript code). So, I have two questions:

我想获取该工作表中的所有行,其中“ID”是 >= 使用 SQL 语句和 ADO 连接(来自 VBScript 代码)的某个值。所以,我有两个问题:

1) Can I convert ID's values to double in SQL? So, then I can compare values as floating point numbers and apply < and > conditions. 2) Can I use in select statement Macro from Excel workbook?

1) 我可以在 SQL 中将 ID 的值转换为 double 吗?因此,我可以将值作为浮点数进行比较并应用 < 和 > 条件。2) 我可以在 Excel 工作簿中使用 select 语句宏吗?

采纳答案by vmg

I found the solution: if I cast column to Integer, I'll have the same effect, so, I used CInt function:

我找到了解决方案:如果我将 column 转换为 Integer,我会有同样的效果,所以,我使用了 CInt 函数:

Select  *  from ["&strSheetName&"$]   where CInt(TestCaseId)  >= " & abs (strRow)

回答by onedaywhen

The CASTto FLOATfunction is CDbl()e.g.

CASTFLOAT功能CDbl()

SELECT CDbl(ID) AS ID_float 
  FROM [Sheet$];

Note DOUBLEis a synonym for FLOAT, hence the contraction Cast to Double

NoteDOUBLE是 的同义词FLOAT,因此C缩写 as to Dou ble

回答by Dunc

try

尝试

WHERE CONVERT (ID, FLOAT) >= value