skip to Main Content

I have following table in postgres

col1 
A018
B672

I would like to add a dot first two character. The expected output is:

col1   col2
A018   A01.8
B672   B67.2

I have tried below query so far

with missed_codes as (
SELECT col1, regexp_replace(col1, '(.{2})(?!$)', '1.') as col2 FROM table 
)
select * from missed_codes ;

I am getting this output

col1   col2
A018   1.18
B672   1.72

Any help is highly appreciated!

2

Answers


  1. For your sample data, you don’t need regex, just left and substr:

    with missed_codes as (
    SELECT col1, left(col1, 3) || '.' || substr(col1, 4) as col2 FROM "table"
    )
    select * from missed_codes 
    

    Output:

    col1    col2
    A018    A01.8
    B672    B67.2
    

    Demo on dbfiddle.uk

    Login or Signup to reply.
  2. If I understand correctly you need to add a dot after first two digits, In this case you need to use regexp_replace :

    SELECT col1, regexp_replace(col1, '^(D*d{2})(?!$)', '1.') as col2 
    FROM "table" 
    

    Demo here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search