0

How can I replace only the the first zeros in a string? Example:

string : '0000089001232100'
result:  '89001232100'

I can't use SQL Server REPLACE function since I don't want all my zeros to be replaced. Thanks

darkdante
  • 707
  • 1
  • 17
  • 36

2 Answers2

3

select substring(ColumnName, patindex('%[^0]%',ColumnName), 10) should give you what you need.

Also look into the RIGHT and LEFT functions. They strip a string from the left or right side, respectively.

If you wanted to use RIGHT, you could get the index of the first 'wanted' character using CHARINDEX(http://msdn.microsoft.com/en-us/library/ms186323.aspx), then use that index as the point to strip from in your RIGHT function.

Mike Baxter
  • 6,868
  • 17
  • 67
  • 115
2

If the string always have only numbers, you could use this:

DECLARE @String VARCHAR(30)
SET @String = '0000089001232100'
SELECT CONVERT(VARCHAR(20),CONVERT(NUMERIC(20,0),@String))
Lamak
  • 69,480
  • 12
  • 108
  • 116