Most efficient method for adding leading 0's to an int in sql

前端 未结 5 523
渐次进展
渐次进展 2021-02-05 17:58

I need to return two fields from a database concatenated as \'field1-field2\'. The second field is an int, but needs to be returned as a fixed length of 5 with leading 0\'s. T

5条回答
  •  粉色の甜心
    2021-02-05 18:35

    If you want to get a consistent number of total strings in the final result by adding different number of zeros, here is a little bit modification (for vsql)

    SELECT 
      CONCAT(
        REPEAT('0', 9-length(TO_CHAR(var1))), 
        CAST(var1 AS VARCHAR(9))
      ) as var1
    

    You can replace 9 by any number for your need!

    BRD

提交回复
热议问题