Storing leading zeros of integers in MySQL database as INTEGER

后端 未结 4 382
渐次进展
渐次进展 2020-12-05 07:32

I need MySQL to store numbers in a integer field and maintain leading zeros. I cannot use the zerofill option as my current field is Bigint(16) and numbers can vary in amoun

4条回答
  •  [愿得一人]
    2020-12-05 08:14

    Keep the numbers stored as integers.

    Then use function LPAD() to show the numbers (left) padded with zeros:

    SELECT LPAD( 14, 7, '0') AS padded;
    
    | padded  |
    -----------
    | 0000014 |
    

    If the number of zerofill characters is variable, add another column in the table with that (zerofill) length.

提交回复
热议问题