Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

I am working on a SQL query that reads from an 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.

1 Answer

0 votes
by (40.7k points)

Try using the below code:

select substring(ColumnName, patindex('%[^0]%',ColumnName), 10)

Enroll yourself in the best online SQL courses to learn in-depth about SQL statements, queries and become proficient in SQL. 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...