强制短文本字段始终为X字符

时间:2017-11-20 15:00:14

标签: ms-access ms-access-2013

我试图在查询中使用IIF()语句将短文本字段更新为始终为4个字符。这是我的语法,但是

SELECT DISTINCT IIf(Len([User ID] = 1), "000" & [User ID], IIf(Len([User ID] = 2), "00" & [User ID], IIf(Len([User ID] = 3), "0" & [User ID], [User ID]))) AS ST
FROM _TestData;

然而,无论长度如何,它似乎都会向数据追加0?

2 个答案:

答案 0 :(得分:2)

可能很简单:

SELECT DISTINCT Right("000" & [User ID], 4) AS ST
FROM TestData;

答案 1 :(得分:0)

如果[User ID]是数字,请尝试以下操作:

SELECT DISTINCT Format([User ID], "0000") AS ST
FROM _TestData;

<强>更新

错过了该字段是短文本。在这种情况下

SELECT DISTINCT Format(CLng(Nz([User ID],0)), "0000") AS ST
FROM _TestData;