SQL查询中的通配符替换
我正在尝试在Access中编写查询。是否可以执行通配符替换(类似这样的操作)SQL查询中的通配符替换,sql,ms-access,Sql,Ms Access,我正在尝试在Access中编写查询。是否可以执行通配符替换(类似这样的操作) 从tblWork中选择替换(MyStatus,,“”) 我有类似的数据:状态:2019年1月1日工作完成。 我想将查询结果转换为:状态:2019年1月1日工作已完成。 注意:我不想更新表中的数据 没有VBA是不行的 因为您可以在查询中使用VBA函数,这当然是可能的。 您可以使用以下功能: Public Function LikeReplace(strInput As String, strPattern As Str
从tblWork中选择替换(MyStatus,,“”)
我有类似的数据:状态:2019年1月1日工作完成。
我想将查询结果转换为:状态:2019年1月1日工作已完成。
注意:我不想更新表中的数据 没有VBA是不行的
因为您可以在查询中使用VBA函数,这当然是可能的。
您可以使用以下功能:
Public Function LikeReplace(strInput As String, strPattern As String, strReplace As String, Optional start As Long = 1)
Dim LenCompare As Long
Do While start <= Len(strInput)
For LenCompare = Len(strInput) - start + 1 To 1 Step -1
If Mid(strInput, start, LenCompare) Like strPattern Then
strInput = Left(strInput, start - 1) & strReplace & Right(strInput, Len(strInput) - (start + LenCompare - 1))
End If
Next
start = start + 1
Loop
LikeReplace = strInput
End Function
Public Function StripDiv(ByVal Input As String) As String
Dim Result As String
If UBound(Split(Input, ">")) > 0 Then
Result = Split(Input, ">")(1)
Else
Result = Input
End If
StripDiv = Result
End Function
但是,性能会受到影响,这既因为VBA函数的性能不高,也因为从查询中调用VBA会导致开销。这不能从外部应用程序中使用
对于更高级的模式匹配,您可以使用使用正则表达式的VBA UDF,这是Krish KM共享的,而不是没有VBA
因为您可以在查询中使用VBA函数,这当然是可能的。
您可以使用以下功能:
Public Function LikeReplace(strInput As String, strPattern As String, strReplace As String, Optional start As Long = 1)
Dim LenCompare As Long
Do While start <= Len(strInput)
For LenCompare = Len(strInput) - start + 1 To 1 Step -1
If Mid(strInput, start, LenCompare) Like strPattern Then
strInput = Left(strInput, start - 1) & strReplace & Right(strInput, Len(strInput) - (start + LenCompare - 1))
End If
Next
start = start + 1
Loop
LikeReplace = strInput
End Function
Public Function StripDiv(ByVal Input As String) As String
Dim Result As String
If UBound(Split(Input, ">")) > 0 Then
Result = Split(Input, ">")(1)
Else
Result = Input
End If
StripDiv = Result
End Function
但是,性能会受到影响,这既因为VBA函数的性能不高,也因为从查询中调用VBA会导致开销。这不能从外部应用程序中使用
对于更高级的模式匹配,您可以使用使用正则表达式的VBA UDF,正如Krish KM所共享的那样。您可以创建一个微型辅助函数:
Public Function LikeReplace(strInput As String, strPattern As String, strReplace As String, Optional start As Long = 1)
Dim LenCompare As Long
Do While start <= Len(strInput)
For LenCompare = Len(strInput) - start + 1 To 1 Step -1
If Mid(strInput, start, LenCompare) Like strPattern Then
strInput = Left(strInput, start - 1) & strReplace & Right(strInput, Len(strInput) - (start + LenCompare - 1))
End If
Next
start = start + 1
Loop
LikeReplace = strInput
End Function
Public Function StripDiv(ByVal Input As String) As String
Dim Result As String
If UBound(Split(Input, ">")) > 0 Then
Result = Split(Input, ">")(1)
Else
Result = Input
End If
StripDiv = Result
End Function
然后:
您可以创建一个微小的辅助函数:
Public Function LikeReplace(strInput As String, strPattern As String, strReplace As String, Optional start As Long = 1)
Dim LenCompare As Long
Do While start <= Len(strInput)
For LenCompare = Len(strInput) - start + 1 To 1 Step -1
If Mid(strInput, start, LenCompare) Like strPattern Then
strInput = Left(strInput, start - 1) & strReplace & Right(strInput, Len(strInput) - (start + LenCompare - 1))
End If
Next
start = start + 1
Loop
LikeReplace = strInput
End Function
Public Function StripDiv(ByVal Input As String) As String
Dim Result As String
If UBound(Split(Input, ">")) > 0 Then
Result = Split(Input, ">")(1)
Else
Result = Input
End If
StripDiv = Result
End Function
然后:
我认为这样的事情可以解决这种情况。即使它不使用通配符
select right( MyStatus
, LEN(MyStatus ) - InStr(MyStatus , '>')
)
from tblWork;
我认为这样的事情可以解决这种情况。即使它不使用通配符
select right( MyStatus
, LEN(MyStatus ) - InStr(MyStatus , '>')
)
from tblWork;
不。如果你需要这样做,我可能会建议你转到Postgres(虽然我不应该推荐数据库)或MariaDB。我不这么认为。试图找到一个快速简单的解决方案来去除所有的HTML标签。如果你需要这样做,我可能会建议你转到Postgres(虽然我不应该推荐数据库)或MariaDB。我不这么认为。试图找到一个快速而简单的解决方案来去除所有的HTML标签。