简体   繁体   English

如何在列中添加前导零?

[英]how do I add a leading zero to a column?

So I am importing a customer table and the date values are missing the leading zero for the month, so month column has a value of 2 instead of 02. How can I add a leading zero to the effected dates without adding a zero to dates where the leading zero is no issue? 因此,我正在导入一个客户表,并且日期值缺少该月的前导零,因此month列的值是2而不是02。如何在不影响日期的情况下将前导零添加到生效日期,而在其中前导零没有问题吗? Any help is appreciated. 任何帮助表示赞赏。 Thanks 谢谢

If you're storing dates, you should be storing them into a date column in MySQL. 如果要存储日期,则应将其存储到MySQL中的date列中。 It'll pad appropriately, and you'll be able to do proper date-based queries on the data like WHERE BETWEEN or using MySQL's INTERVAL math. 它将适当地填充,并且您将能够对数据进行适当的基于日期的查询,例如WHERE BETWEEN或使用MySQL的INTERVAL数学。

Try select right(concat('0', <YourMonth>), 2) . 尝试select right(concat('0', <YourMonth>), 2)

Alternatively you could use the MySQL LPAD function, but this is a handy technique to know that you can also use in Excel. 另外,您可以使用MySQL LPAD函数,但这是一种方便的技巧,它也可以在Excel中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM