Remove Leading Zeros

Posted: October 1, 2010 in SQL Server 2005

To remove leading zeros from a string, you can use the PATINDEX command as follows:

-- Declare variables
DECLARE @lv_string VARCHAR(2000);

-- Intialise variables
SET @lv_string = '00000TestingLeadingZeros';

-- Use PATINDEX to remove leading zeros
SELECT SUBSTRING(RTRIM(LTRIM(@lv_string)), PATINDEX('%[^0]%', RTRIM(LTRIM(@lv_string)) + '1'), LEN(RTRIM(LTRIM(@lv_string))));  

You can change the PATINDEX part for your own requirements.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s