0

尝试在 MS Access 中尝试以下操作。

将一个字段中的数据转换为另一个字段中以 01 开头的 18 位数字。

还有一些条件必须满足:

  • 第一个破折号应该变成双零
  • 第二个破折号应该被删除
  • 第三个和第四个破折号应该是一个零
  • 小数点也必须用零替换

我的查询工作正常,直到小数点是数据中的第 15 个字符。

这是查询:

SELECT MasterVacant.ParcelIdNumber,
    "01" + Mid([ParcelIdNumber],1,2) + "00" + Mid([ParcelIdNumber],4,2) + Mid([ParcelIdNumber],7,1)
        + IIf(Mid([ParcelIDNumber],11,1) = "", "0"+Mid([ParcelIDNumber],9,2), Mid([ParcelIDNumber],9,3))
        + IIf(Mid([ParcelIDNumber],14,1) = ".", "0"+Mid([ParcelIDNumber],12,2), Mid([ParcelIDNumber],12,3))
        + Mid([ParcelIDNumber],15,3) AS ParcelNumber
FROM MasterVacant;

这是一个开始和结束的例子......

'12-06-1-00-50.000-RR'  should become '011200061000050000'
'12-06-3-07-09.000-RR'  should become '011200063007009000'
'13-35-1-01-129.000-RR' should become '011300035100112900'

但是,我得到的不是“0113000351001129000”,而是“013000351001129.00”。

问题是当小数点是第 15 个字符时,如何删除小数点,就像第三组示例中一样?

我将数据作为单列接收。部分内容如下......

1. 13-35-1-07-29.000-RR 
2. 13-35-1-01-112.000-RR (Removing the decimal when the data is like this is the issue)
3. 13-35-4-01-01.000-RR
4. 13-35-4-02-04.000-RR
5. 13-35-1-13-17.000-RR

上述数据的输出应该是

1. 011300351007029000
2. 011300351001112000
3. 011300354001001000
4. 011300354002004000
5. 011300351013017000
4

2 回答 2

0

我假设您的意思相反:

12-06-1-00-50.000-RR should become 011200061000050000 
12-06-3-07-09.000-RR should become 011200063007009000 
13-35-1-01-129.000-RR should become 0113000351001129.00

我会推荐REPLACE()inMSACCESS去掉破折号。一旦你有破折号,你可以MID()

不幸的是,您尝试的代码对第三行做了一些不同的事情,因为在我认为应该只有两个的时候,却输入了 3 个零。

在文本框中尝试:

=Replace("13-35-1-01-129.000-RR","-","")

将返回1335101129.000RR 并查看这是否有助于您编写代码。

也许更进一步,把它放在一个函数中。

于 2020-03-06T05:44:07.617 回答
0

使用自定义函数:

Public Function Make18(ByVal Value As String) As String

    Const Head  As String = "01"
    Const Tail  As String = "000"
    Const Lead  As String = "00"

    Dim Parts   As Variant
    Dim Part    As Integer
    Dim Result  As String

    Parts = Split(Split(Value, ".")(0), "-")

    For Part = LBound(Parts) To UBound(Parts)
        Select Case Part
            Case 0
                Parts(Part) = Head & Parts(Part)
            Case 1
                Parts(Part) = Lead & Parts(Part)
            Case 3, 4
                Parts(Part) = Right(Lead & Parts(Part), 3)
        End Select
    Next

    Result = Join(Parts, "") & Tail

    Make18 = Result

End Function

你的查询变成:

SELECT 
    MasterVacant.ParcelIdNumber,
    Make18([ParcelIdNumber]) AS ParcelNumber
FROM 
    MasterVacant;
于 2020-03-06T08:50:48.877 回答