Is there a MySQL equivalent of sprintf?

前端 未结 3 458
轮回少年
轮回少年 2021-01-17 08:05

I have an INT field in my table, which I\'d like to select as a zero-padded string. So for example, 8 would come out as 008, 23 as 023 and so on. I

3条回答
  •  隐瞒了意图╮
    2021-01-17 08:07

    If you want to achieve a minimum number of padding, without cutting the results of larger numbers, you'll need to use an IF statement.

    The example below will make sure all IDs have a minimum of three digits, while letting larger IDs still pass through untrimmed.

    SELECT IF(id < 100, LPAD(id, 3, 0), id)
    

提交回复
热议问题