如何在 Access SQL 中将空字符串转换为数字 null
How do I convert an empty string into a numerical null in Access SQL_
我想用暂存 table 中的数据填充 table。 staging table 中有趣的列具有数据类型 text
,但在其他方面填充了可解析为双精度值或空字符串(即 "4.209"
、"42"
或""
)。目标 table 中的相应列的数据类型为 double
.
我正在执行的SQL语句是
insert into dest (.., theColumn, ... ) select ...., theColumn, .. from src
当我执行语句(使用 ADO)时收到 Data type mismatch in criteria expression
错误。
如果我用 null
替换 theColumn
,它可以正常工作。所以,我认为我应该以某种方式将空字符串转换为 null
s。这可能吗?
将空字符串转换为零也可能有效。
insert into dest (.., theColumn, ... ) select ...., theColumn+0, .. from src
您可以使用 Val 将 "" 转换为 0:
insert into dest (.., theColumn, ... ) select ...., Val(theColumn), .. from src
要插入 Null,请使用如下函数:
' Converts a value of any type to its string representation.
' The function can be concatenated into an SQL expression as is
' without any delimiters or leading/trailing white-space.
'
' Examples:
' SQL = "Select * From TableTest Where [Amount]>" & CSql(12.5) & "And [DueDate]<" & CSql(Date) & ""
' SQL -> Select * From TableTest Where [Amount]> 12.5 And [DueDate]< #2016/01/30 00:00:00#
'
' SQL = "Insert Into TableTest ( [Street] ) Values (" & CSql(" ") & ")"
' SQL -> Insert Into TableTest ( [Street] ) Values ( Null )
'
' Trims text variables for leading/trailing Space and secures single quotes.
' Replaces zero length strings with Null.
' Formats date/time variables as safe string expressions.
' Uses Str to format decimal values to string expressions.
' Returns Null for values that cannot be expressed with a string expression.
'
' 2016-01-30. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function CSql( _
ByVal Value As Variant) _
As String
Const vbLongLong As Integer = 20
Const SqlNull As String = " Null"
Dim Sql As String
Dim LongLong As Integer
#If Win32 Then
LongLong = vbLongLong
#End If
#If Win64 Then
LongLong = VBA.vbLongLong
#End If
Select Case VarType(Value)
Case vbEmpty ' 0 Empty (uninitialized).
Sql = SqlNull
Case vbNull ' 1 Null (no valid data).
Sql = SqlNull
Case vbInteger ' 2 Integer.
Sql = Str(Value)
Case vbLong ' 3 Long integer.
Sql = Str(Value)
Case vbSingle ' 4 Single-precision floating-point number.
Sql = Str(Value)
Case vbDouble ' 5 Double-precision floating-point number.
Sql = Str(Value)
Case vbCurrency ' 6 Currency.
Sql = Str(Value)
Case vbDate ' 7 Date.
Sql = Format(Value, " \#yyyy\/mm\/dd hh\:nn\:ss\#")
Case vbString ' 8 String.
Sql = Replace(Trim(Value), "'", "''")
If Sql = "" Then
Sql = SqlNull
Else
Sql = " '" & Sql & "'"
End If
Case vbObject ' 9 Object.
Sql = SqlNull
Case vbError ' 10 Error.
Sql = SqlNull
Case vbBoolean ' 11 Boolean.
Sql = Str(Abs(Value))
Case vbVariant ' 12 Variant (used only with arrays of variants).
Sql = SqlNull
Case vbDataObject ' 13 A data access object.
Sql = SqlNull
Case vbDecimal ' 14 Decimal.
Sql = Str(Value)
Case vbByte ' 17 Byte.
Sql = Str(Value)
Case LongLong ' 20 LongLong integer (Valid on 64-bit platforms only).
Sql = Str(Value)
Case vbUserDefinedType ' 36 Variants that contain user-defined types.
Sql = SqlNull
Case vbArray ' 8192 Array.
Sql = SqlNull
Case Else ' Should not happen.
Sql = SqlNull
End Select
CSql = Sql & " "
End Function
使用IIf()
表达式:如果theColumn
包含一个表示有效数字的字符串,return该数字;否则 return 为空。
SELECT IIf(IsNumeric(theColumn), Val(theColumn), Null) FROM src
我的第一个冲动是使用 IsNumeric()
。但是我意识到这是对您要求的更直接的翻译...
SELECT IIf(theColumn='', Null, Val(theColumn)) FROM src
我想用暂存 table 中的数据填充 table。 staging table 中有趣的列具有数据类型 text
,但在其他方面填充了可解析为双精度值或空字符串(即 "4.209"
、"42"
或""
)。目标 table 中的相应列的数据类型为 double
.
我正在执行的SQL语句是
insert into dest (.., theColumn, ... ) select ...., theColumn, .. from src
当我执行语句(使用 ADO)时收到 Data type mismatch in criteria expression
错误。
如果我用 null
替换 theColumn
,它可以正常工作。所以,我认为我应该以某种方式将空字符串转换为 null
s。这可能吗?
将空字符串转换为零也可能有效。
insert into dest (.., theColumn, ... ) select ...., theColumn+0, .. from src
您可以使用 Val 将 "" 转换为 0:
insert into dest (.., theColumn, ... ) select ...., Val(theColumn), .. from src
要插入 Null,请使用如下函数:
' Converts a value of any type to its string representation.
' The function can be concatenated into an SQL expression as is
' without any delimiters or leading/trailing white-space.
'
' Examples:
' SQL = "Select * From TableTest Where [Amount]>" & CSql(12.5) & "And [DueDate]<" & CSql(Date) & ""
' SQL -> Select * From TableTest Where [Amount]> 12.5 And [DueDate]< #2016/01/30 00:00:00#
'
' SQL = "Insert Into TableTest ( [Street] ) Values (" & CSql(" ") & ")"
' SQL -> Insert Into TableTest ( [Street] ) Values ( Null )
'
' Trims text variables for leading/trailing Space and secures single quotes.
' Replaces zero length strings with Null.
' Formats date/time variables as safe string expressions.
' Uses Str to format decimal values to string expressions.
' Returns Null for values that cannot be expressed with a string expression.
'
' 2016-01-30. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function CSql( _
ByVal Value As Variant) _
As String
Const vbLongLong As Integer = 20
Const SqlNull As String = " Null"
Dim Sql As String
Dim LongLong As Integer
#If Win32 Then
LongLong = vbLongLong
#End If
#If Win64 Then
LongLong = VBA.vbLongLong
#End If
Select Case VarType(Value)
Case vbEmpty ' 0 Empty (uninitialized).
Sql = SqlNull
Case vbNull ' 1 Null (no valid data).
Sql = SqlNull
Case vbInteger ' 2 Integer.
Sql = Str(Value)
Case vbLong ' 3 Long integer.
Sql = Str(Value)
Case vbSingle ' 4 Single-precision floating-point number.
Sql = Str(Value)
Case vbDouble ' 5 Double-precision floating-point number.
Sql = Str(Value)
Case vbCurrency ' 6 Currency.
Sql = Str(Value)
Case vbDate ' 7 Date.
Sql = Format(Value, " \#yyyy\/mm\/dd hh\:nn\:ss\#")
Case vbString ' 8 String.
Sql = Replace(Trim(Value), "'", "''")
If Sql = "" Then
Sql = SqlNull
Else
Sql = " '" & Sql & "'"
End If
Case vbObject ' 9 Object.
Sql = SqlNull
Case vbError ' 10 Error.
Sql = SqlNull
Case vbBoolean ' 11 Boolean.
Sql = Str(Abs(Value))
Case vbVariant ' 12 Variant (used only with arrays of variants).
Sql = SqlNull
Case vbDataObject ' 13 A data access object.
Sql = SqlNull
Case vbDecimal ' 14 Decimal.
Sql = Str(Value)
Case vbByte ' 17 Byte.
Sql = Str(Value)
Case LongLong ' 20 LongLong integer (Valid on 64-bit platforms only).
Sql = Str(Value)
Case vbUserDefinedType ' 36 Variants that contain user-defined types.
Sql = SqlNull
Case vbArray ' 8192 Array.
Sql = SqlNull
Case Else ' Should not happen.
Sql = SqlNull
End Select
CSql = Sql & " "
End Function
使用IIf()
表达式:如果theColumn
包含一个表示有效数字的字符串,return该数字;否则 return 为空。
SELECT IIf(IsNumeric(theColumn), Val(theColumn), Null) FROM src
我的第一个冲动是使用 IsNumeric()
。但是我意识到这是对您要求的更直接的翻译...
SELECT IIf(theColumn='', Null, Val(theColumn)) FROM src