IIf 查询小数点去除

IIf query decimal removal

正在尝试在 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

我假设你的意思正好相反:

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

我会推荐 MSACCESS 中的 REPLACE() 去掉破折号。完成破折号后,您可以 MID()

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

在文本框中尝试:

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

将return1335101129.000RR 看看这是否有助于您编写代码。

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

使用自定义函数:

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;