How to Remove Leading Zeros from a Column in SQL

When working with SQL databases, you might encounter numbers that contain leading zeros, such as 000123 or 0000456. While these zeros don't change the actual value of the number, they can make your data look unclean or cause issues when you're using the data for calculations or display purposes.

In this blog, we'll walk through a few techniques to remove leading zeros from a column in SQL.

Why Remove Leading Zeros?

Leading zeros can make data less readable or cause issues in your applications. Imagine having a table of product IDs that looks like this:

000123  
000456  
00000789

By removing the leading zeros, we can clean up the data to look like this:

123  
456  
789

This cleaner format is easier to work with and interpret, especially for reports or data analysis.

Sample Data

  • Let’s consider a table called employees with the following structure:
| employee_id | employee_name  |
|-------------|----------------|
| 000123      | Vishal Patel   |
| 0000456     | Jane Smith     |
| 00000789    | Alice Johnson  |

Our goal is to remove the leading zeros from the employee_id column.

Method 1: Using CAST() to Convert to a Number

  • The simplest way to remove leading zeros is to convert the text into a number. SQL automatically drops leading zeros when converting a string of digits to an integer.
SELECT 
    CAST(employee_id AS INT) AS employee_id_cleaned, 
    employee_name 
FROM 
    employees;

Explanation:

  • CAST(employee_id AS INT): This converts the employee_id from a string to an integer. Since integers don’t store leading zeros, the result will be clean, numeric values.

Result:

| employee_id_cleaned | employee_name  |
|---------------------|----------------|
| 123                 | Vishal Patel   |
| 456                 | Jane Smith     |
| 789                 | Alice Johnson  |
  • When to use this method: Use this when the column contains purely numeric values.

Method 2: Using LTRIM() to Remove Zeros

  • If the column contains alphanumeric or decimal values (e.g., 00AB123`, ‘0050.03’), using CAST() won’t work. In such cases, we can use the LTRIM() function to remove the leading zeros from the text.
SELECT 
    LTRIM(employee_id, '0') AS employee_id_cleaned, 
    employee_name 
FROM 
    employees;

Explanation:

  • LTRIM(employee_id, '0'): This removes all leading zeros from the left side of the employee_id. If the value is 000123.23, it will become 123.23. If it's 00AB123, it will become AB123.

Result:

| employee_id_cleaned | employee_name  |
|---------------------|----------------|
| 123                 | Vishal Patel   |
| 456                 | Jane Smith     |
| 789                 | Alice Johnson  |
  • When to use this method: Use this when the column contains decimal or alphanumeric values.

Method 3: Combining LTRIM() and REPLACE() for Complex Cleaning

  • In some cases, you may need to clean up more complex data that contains both leading zeros and spaces or other characters. The REPLACE() function can help you further refine the data.
SELECT 
    REPLACE(LTRIM(employee_id, '0'), ' ', '') AS employee_id_cleaned, 
    employee_name 
FROM 
    employees;

Explanation:

  • LTRIM(employee_id, '0'): This removes leading zeros.

  • REPLACE(employee_id, ' ', ''): After trimming, this ensures that any unexpected spaces in the data are removed.

Result:

| employee_id_cleaned | employee_name  |
|---------------------|----------------|
| 123                 | Vishal Patel   |
| 456                 | Jane Smith     |
| 789                 | Alice Johnson  |
  • When to use this method: Use this when you have more complex values that might contain spaces or other unwanted characters in addition to leading zeros.

Method 4: Using RIGHT() to Ensure a Specific Length (Advanced)

  • If your goal is to ensure that the cleaned data always has a certain length (e.g., you want to keep a minimum of 3 digits in the employee_id), you can use the RIGHT() function. This will trim leading zeros but still allow you to keep a fixed number of characters.
SELECT 
    RIGHT(LTRIM(employee_id, '0'), 3) AS employee_id_cleaned, 
    employee_name 
FROM 
    employees;

Explanation:

  • LTRIM(employee_id, '0'): Removes the leading zeros.

  • RIGHT(employee_id, 3)`: Ensures that the resulting value always has at least 3 characters, taking the rightmost characters if necessary.

Result:

| employee_id_cleaned | employee_name  |
|---------------------|----------------|
| 123                 | VIshal Patel   |
| 456                 | Jane Smith     |
| 789                 | Alice Johnson  |
  • When to use this method: Use this when you want to ensure a specific format for the employee_id.

Method 5: Handling NULL or Empty Values

  • While working with real-world data, it's common to encounter NULL or empty values. To prevent errors or unwanted results when removing leading zeros, it’s a good idea to handle NULL values explicitly.
SELECT 
    CASE 
        WHEN employee_id IS NULL OR employee_id = '' THEN NULL 
        ELSE LTRIM(employee_id, '0') 
    END AS employee_id_cleaned, 
    employee_name 
FROM 
    employees;

Explanation:

  • CASE WHEN: This checks if employee_id is NULL or an empty string and returns NULL in those cases.

  • LTRIM(employee_id, '0'): If employee_id has a value, this removes the leading zeros.

Result:

| employee_id_cleaned | employee_name  |
|---------------------|----------------|
| 123                 | Vishal Patel   |
| 456                 | Jane Smith     |
| 789                 | Alice Johnson  |
  • When to use this method: Use this when your data might contain NULL or empty values.

Conclusion

Removing leading zeros from a column in SQL can be done in several ways depending on your data:

  • CAST(): Best for purely numeric columns.

  • LTRIM(): Great for decimal or alphanumeric values.

  • REPLACE(): Useful for cleaning up spaces or other unwanted characters.

  • RIGHT(): Helps maintain a specific format by ensuring a fixed length.

  • Handling NULL: Ensures that null values are properly handled.

Each of these methods can help clean your data, making it more readable and easier to work with!

0
Subscribe to my newsletter

Read articles from Vishal Barvaliya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vishal Barvaliya
Vishal Barvaliya