MS-Access中的排序
我想在MS Access中对数据进行排序,为此我使用了一个查询。我的表格中的数据如下所示:MS-Access中的排序,ms-access,ms-access-2007,Ms Access,Ms Access 2007,我想在MS Access中对数据进行排序,为此我使用了一个查询。我的表格中的数据如下所示: RadButtonNo ------------------- AA001056 AA001579 B000049 AA001261 AA001158 AA001108 AA001166 AA001165 AA001164 AA001163 AA001162 对于我的输出,我首先想要的数据将只是由
RadButtonNo
-------------------
AA001056
AA001579
B000049
AA001261
AA001158
AA001108
AA001166
AA001165
AA001164
AA001163
AA001162
对于我的输出,我首先想要的数据将只是由字母组成的数据。接下来,我想显示字母和数字。所以,它看起来像这样:
AAAAAAA
AABBBBB
AAZZZZZ
ABA1001
我正在使用以下查询:
SELECT RadButtonNo, ShortName, InspectionDate, Findings, Status, QueryForNot1.Initials, DeptName, Lost, TableApron.InServelDate, TableApron.RemovedDate,
TableApron.PrivateUserName, TableApron.PrivateUserEmail, TableApron.ApronType, TableApron.Manufacturer
FROM TableApron
LEFT JOIN QueryForNot1 ON TableApron.RadButtonNo=QueryForNot1.RadButtonNoI
WHERE (((TableApron.Lost)="N" Or (TableApron.Lost)=[@Lost])
ORDER BY LEN(TableApron.RadButtonNo) DESC , TableApron.RadButtonNo;
有人能解决这个问题,让它产生我想要的输出吗 您可以使用以下两个功能:
Public Function TrimNumString( _
ByVal strNumString As String, _
Optional ByVal strDecimalChr As String, _
Optional ByVal booAcceptMinus As Boolean) _
As String
' Removes any non-numeric character from strNumString including hexadecimal characters.
' If strDecimalChr is specified, first occurrence of this is not removed.
' If booAcceptMinus is True, a leading or trailing minus sign is accepted.
'
' 1999-08-27. Cactus Data ApS, CPH.
' 2001-06-21. Speed optimized for large string (64 K).
' 2003-12-10. intOffset changed to lngOffset.
Const cbytNeg As Byte = 45 ' "-"
Dim lngPos As Long
Dim lngLen As Long
Dim lngOffset As Long
Dim booDec As Boolean
Dim booNeg As Boolean
Dim bytChr As Byte
Dim bytDec As Byte
Dim strNum As String
strNumString = Trim(strNumString)
lngLen = Len(strNumString)
If lngLen > 0 Then
If Len(strDecimalChr) > 0 Then
bytDec = Asc(strDecimalChr)
End If
' Create empty result string of maximum possible length.
strNum = Space(lngLen)
For lngPos = 1 To lngLen
bytChr = Asc(Mid(strNumString, lngPos, 1))
Select Case bytChr
Case 48 To 57
' Digit.
Case bytDec
' Decimal point.
If booDec = False Then
' One decimal point only.
booDec = True
End If
Case cbytNeg
' Minus sign.
bytChr = 0
If booAcceptMinus = True And booNeg = False Then
If Len(Trim(strNum)) = 0 Or lngPos = lngLen Then
bytChr = cbytNeg
' One minus sign only.
booNeg = True
End If
End If
Case Else
' Ignore any other character.
bytChr = 0
End Select
If bytChr > 0 Then
' Append accepted character by inserting it in result string.
lngOffset = lngOffset + 1
Mid(strNum, lngOffset) = Chr(bytChr)
End If
Next
End If
' Trim and return result string.
TrimNumString = Left(strNum, lngOffset)
End Function
Public Function TrimTxtString( _
ByVal strTxtString As String) _
As String
' Removes any numeric character from strTxtString.
'
' 2003-12-19. Cactus Data ApS, CPH.
Dim lngPos As Long
Dim lngLen As Long
Dim lngOffset As Long
Dim bytChr As Byte
Dim strNum As String
strTxtString = Trim(strTxtString)
lngLen = Len(strTxtString)
If lngLen > 0 Then
' Create empty result string of maximum possible length.
strNum = Space(lngLen)
For lngPos = 1 To lngLen
bytChr = Asc(Mid(strTxtString, lngPos, 1))
Select Case bytChr
Case 48 To 57
' Digit.
bytChr = 0
Case Else
' Accept any other character.
End Select
If bytChr > 0 Then
' Append accepted character by inserting it in result string.
lngOffset = lngOffset + 1
Mid(strNum, lngOffset) = Chr(bytChr)
End If
Next
End If
' Trim and return result string.
TrimTxtString = Left(strNum, lngOffset)
End Function
然后调整SQL:
ORDER BY LEN(TableApron.RadButtonNo) DESC, TrimTxtString([TableApron].[RadButtonNo]), TrimNumString([TableApron].[RadButtonNo]);
您可以使用以下两个功能:
Public Function TrimNumString( _
ByVal strNumString As String, _
Optional ByVal strDecimalChr As String, _
Optional ByVal booAcceptMinus As Boolean) _
As String
' Removes any non-numeric character from strNumString including hexadecimal characters.
' If strDecimalChr is specified, first occurrence of this is not removed.
' If booAcceptMinus is True, a leading or trailing minus sign is accepted.
'
' 1999-08-27. Cactus Data ApS, CPH.
' 2001-06-21. Speed optimized for large string (64 K).
' 2003-12-10. intOffset changed to lngOffset.
Const cbytNeg As Byte = 45 ' "-"
Dim lngPos As Long
Dim lngLen As Long
Dim lngOffset As Long
Dim booDec As Boolean
Dim booNeg As Boolean
Dim bytChr As Byte
Dim bytDec As Byte
Dim strNum As String
strNumString = Trim(strNumString)
lngLen = Len(strNumString)
If lngLen > 0 Then
If Len(strDecimalChr) > 0 Then
bytDec = Asc(strDecimalChr)
End If
' Create empty result string of maximum possible length.
strNum = Space(lngLen)
For lngPos = 1 To lngLen
bytChr = Asc(Mid(strNumString, lngPos, 1))
Select Case bytChr
Case 48 To 57
' Digit.
Case bytDec
' Decimal point.
If booDec = False Then
' One decimal point only.
booDec = True
End If
Case cbytNeg
' Minus sign.
bytChr = 0
If booAcceptMinus = True And booNeg = False Then
If Len(Trim(strNum)) = 0 Or lngPos = lngLen Then
bytChr = cbytNeg
' One minus sign only.
booNeg = True
End If
End If
Case Else
' Ignore any other character.
bytChr = 0
End Select
If bytChr > 0 Then
' Append accepted character by inserting it in result string.
lngOffset = lngOffset + 1
Mid(strNum, lngOffset) = Chr(bytChr)
End If
Next
End If
' Trim and return result string.
TrimNumString = Left(strNum, lngOffset)
End Function
Public Function TrimTxtString( _
ByVal strTxtString As String) _
As String
' Removes any numeric character from strTxtString.
'
' 2003-12-19. Cactus Data ApS, CPH.
Dim lngPos As Long
Dim lngLen As Long
Dim lngOffset As Long
Dim bytChr As Byte
Dim strNum As String
strTxtString = Trim(strTxtString)
lngLen = Len(strTxtString)
If lngLen > 0 Then
' Create empty result string of maximum possible length.
strNum = Space(lngLen)
For lngPos = 1 To lngLen
bytChr = Asc(Mid(strTxtString, lngPos, 1))
Select Case bytChr
Case 48 To 57
' Digit.
bytChr = 0
Case Else
' Accept any other character.
End Select
If bytChr > 0 Then
' Append accepted character by inserting it in result string.
lngOffset = lngOffset + 1
Mid(strNum, lngOffset) = Chr(bytChr)
End If
Next
End If
' Trim and return result string.
TrimTxtString = Left(strNum, lngOffset)
End Function
然后调整SQL:
ORDER BY LEN(TableApron.RadButtonNo) DESC, TrimTxtString([TableApron].[RadButtonNo]), TrimNumString([TableApron].[RadButtonNo]);
通过在条目后面添加一个或多个数字对条目进行分组。对那个新领域进行排序。我在这里使用一个带所有z和额外z的值来排除一个真正的z条目
SELECT RadButtonNo
from tbl
order by IIF(RadButtonNo like "*#*", "zzzzzzzzz" & RadButtonNo, RadButtonNo)
通过在条目后面添加一个或多个数字对条目进行分组。对那个新领域进行排序。我在这里使用一个带所有z和额外z的值来排除一个真正的z条目
SELECT RadButtonNo
from tbl
order by IIF(RadButtonNo like "*#*", "zzzzzzzzz" & RadButtonNo, RadButtonNo)
到底是什么问题?请重新阅读并编辑您的问题,这很难理解。@Andre451我编辑了这个问题,您能理解吗?字段的正常排序将作为您的示例进行排序,那么您有什么问题吗?@Gustav我希望先从字符串A短到Z,然后从A(如1,2等数字)开始示例-
AAAA
,AAAB
在用数字示例完成这篇文章之后,像这样-'AAA1,
AAA2`到底是什么问题?请重新阅读并编辑您的问题,这很难理解。@Andre451我编辑了这个问题,您能理解吗?字段的正常排序将作为您的示例进行排序,那么您有什么问题吗?@Gustav我希望先从字符串A短到Z,然后从A(如1,2等数字)开始示例-AAAA
,AAAB
像这样在用数字示例完成本A后-'AAA1,
AAA2`我希望这可以在ms access中的sql脚本中完成,我们可以只添加ms access标记吗我希望这可以在ms access中的sql脚本中完成,我们可以只添加ms access标记吗