2

I have a column that has both numbers and their units and I am trying to split it into two columns with regex, but I don't know how to use regex with split_part. Can anyone advise?

This is an example column:

|    lot_size1    |
---------+---+-----
| 25,665 Lot SqFt |
| 4,111 Lot SqFt  |
| 14,000 Lot SqFt |
|  Lot SqFt       |
| 40.00 Acres     |

This is a draft of my query:

select split_part(lot_size1, ???,1) as area,
  split_part(lot_size1, ???,2) as units;

1 Answer 1

2

use SUSBTRING

select SUBSTRING ( lot_size1 FROM  '^[0-9,.]+' ) as area ,
       SUBSTRING ( lot_size1 FROM  '[^0-9,.]+' ) as units
       FROM t;

'^[0-9,.]+' - matches sequence of numbers , and .

'[^0-9,.]+' - matches sequence of anything other than above.

Demo

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! How would I modify this query to change any empty area fields to NULL?
@reallymemorable : It is in fact null. Run \pset null '(null)' if you're using psql to see it.
How would I eliminate the empty space that appears before the units text?
@reallymemorable : use trim()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.