0

Hi I need to transform the following string as follows:

Before: 6621 Lake Valley DrMemphis, TN 38141

After: 6621 Lake Valley Dr

Additional ways the data comes in: 143 Evergreen Forest Court (this one is fine as is)

Need to extract address only for these as well:

  1. 326 Hambrick Park Fayetteville, GA 30215
  2. RE: Owner's Policy - 112 Shagbark Ln Mooresville, NC 28115
  3. RE: Owner's Policy - 540 Clearbrook Dr Covington, GA 30016
  4. Closed 9/1/21 4421 Home Stakes Dr Parkton, NC 28371
  5. RP 9/16- 352 Hampton St Elloree, SC 29047
  6. RP: 9/15- 124 Lake Grove Rd Simpsonville, SC 29681
  7. FHA 3/2/22- 6083 Holiday Blvd Forest Park, GA 30297
  8. RD 10/1/21 Roxanne Sellers- 311 Woodbrook Ln Marietta, GA 30068
  9. 4104 Flat Trl- Ricardo Reeder

How can I accomplish this using Snowflake SQL? I'm assuming regex_replace is in order? Can anybody help me out?

1
  • Can you add more examples of the transformation you need? Commented Jan 28, 2022 at 6:59

2 Answers 2

1

If you only give us one example, we can only solve for that example:

select regexp_replace(x, '(.*[a-z])([A-Z].*)', '\\1')
from (select '6621 Lake Valley DrMemphis, TN 38141' x)

If you need help with a more general case, please start a new question with more sample inputs and outputs.

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

2 Comments

Good call, Felipe! Here's some other examples of how the inputs look: 1306 Eddie Craig Ct McDonough, GA 30252 143 Evergreen Forest Court (this one is fine as is). I made the edits above
Ok your solution helped with one of my issues, but doesn't work for the strings that look like this: 7401 Bosson St SW Concord, NC 28025
0

We can try using REGEXP_REPLACE with the help of a capture group:

WITH yourTable AS (
    SELECT '6621 Lake Valley DrMemphis, TN 38141' AS address
)

SELECT REGEXP_REPLACE(address, '(\d+( [A-Z][a-z]+)*).*', '\1') AS address_out
FROM yourTable;

Demo

The above working demo is for Postgres, not Snowflake. You might have to use $1 instead of \1 for the first capture group, but the rest should run unaltered.

3 Comments

That resulted in: Invalid regular expression: '(d+(?: [A-Z][a-z]+)*).*', no argument for repetition operator: ? I removed the ? and it didn't result in my desired output (just gave me my original string back)
Try the updated version.
Still gave me my original string back. Also tried it with $1 and same thing :/

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.