Home >Database >Mysql Tutorial >How to Remove Leading Zeros from a VARCHAR Field in SQL?
Tricks to remove leading zeros in SQL
When extracting data from the database, you often encounter fields with leading zeros, which brings inconvenience to data processing. This article explains how to remove leading zeros from VARCHAR fields using SQL.
Suppose there is a field called "CustomerNumber" with data like '00001A'. To eliminate leading zeros, you can use the following method:
<code class="language-sql">SELECT SUBSTRING(CustomerNumber, PATINDEX('%[^0]%', CustomerNumber), 10)</code>
This statement uses the SUBSTRING function to extract non-zero characters in the "CustomerNumber" field. Here’s how it works:
The above is the detailed content of How to Remove Leading Zeros from a VARCHAR Field in SQL?. For more information, please follow other related articles on the PHP Chinese website!