Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing leading zeroes from a field in a SQL statement

I am working on a SQL query that reads from a SQLServer database to produce an extract file. One of the requirements to remove the leading zeroes from a particular field, which is a simple VARCHAR(10) field. So, for example, if the field contains '00001A', the SELECT statement needs to return the data as '1A'.

Is there a way in SQL to easily remove the leading zeroes in this way? I know there is an RTRIM function, but this seems only to remove spaces.

like image 365
Tim C Avatar asked Sep 18 '08 12:09

Tim C


3 Answers

select substring(ColumnName, patindex('%[^0]%',ColumnName), 10)
like image 180
Ian Horwill Avatar answered Oct 22 '22 02:10

Ian Horwill


select replace(ltrim(replace(ColumnName,'0',' ')),' ','0')
like image 29
MTZ Avatar answered Oct 22 '22 02:10

MTZ


You can use this:

SELECT REPLACE(LTRIM(REPLACE('000010A', '0', ' ')),' ', '0')
like image 6
Stelian Avatar answered Oct 22 '22 01:10

Stelian