Question

I have an SQL query that brings back 17 numbers with this format

06037-11

I need to add a 0 before the dash, so it is:

060370-11

Is there an easy way to do this? I have seen STUFF() as an option, but I don't understand it.

Edit I am using Teradata

Was it helpful?

Solution 2

Previous response includes example for Teradata 14.x using regular expression support. The following will work in Teradata 13.x or Teradata 12.x without regular expression support:

SELECT SUBSTRING('06037-11' FROM 1 FOR (POSITION('-' IN '06037-11') -1))
    || '0-'
    || SUBSTRING('06037-11' FROM (POSITION('-' IN '06037-11') + 1))

OTHER TIPS

One way in Oracle:

with qry as
  (select '06037-11' code from dual)
select regexp_replace(code, '-', '0-') from qry;

since you talk about STUFF I assue you use SQL Server:

select stuff(your_column, charindex('-', your_column), 0, '0')
from your_table

SQLFiddle demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top