我有一个数据表:
000001
123
00123
123456
1234
mydog
04456mydog
我希望所有数字都<10000
格式化为四位数,如下所示:
0001
0123
0123
123456
1234
mydog
04456mydog
我怎样才能做到这一点?
一种可能的方法:
UPDATE test
SET digits = LPAD(CAST(digits AS UNSIGNED), 4, '0')
WHERE digits REGEXP '^[0-9]+$'
AND CAST(digits AS UNSIGNED) < 10000;
未经测试。这可以工作:
select case when YourField REGEXP '^-?[0-9]+$' and cast(YourField as unsigned) < 10000 then LPAD(RIGHT(YourField, 4), 4, '0')
else YourField
end
from YourTable