0

I'm looking to isolate street names from addresses by removing the building number from the start of the address. In some cases, there is no building number, so the address begins with a numbered street. The query needs to be used in a view.

e.g.

Order Key Address
1 1990 bismuth ave.
2 61st st.

to:

Order Key Street Name
1 bismuth ave.
2 61st st.

My initial attempt was to use string_split to separate the address by spaces, then select only those rows containing an alphabetic character, then to aggregate them back together grouping over their shared order key.

The problem I'm running into is formatting this into a sequence that doesn't involve using one or more ctes, or nested selects with an aggregate, both of which are throwing errors. This is the version with CTEs, which works, but I'm trying to create this street name column within the view select statement:

WITH street_name_cte AS (
SELECT [ORDER_KEY]
      ,[Address]
      ,value as SPLIT_STRING
      
FROM [TABLE]
CROSS APPLY STRING_SPLIT([Address], ' ')
WHERE value LIKE '%[abcdefghijklmnopqrstuvwxyz]%'
)

SELECT string_agg(SPLIT_STRING, ' ')
FROM street_name_cte
GROUP BY [ORDER_KEY]

This gets the results I need, but I'd like to find a way to format it to add the cleaned street name column to an existing table without ctes in order to include it in-line in a larger select statement which creates a view.

  • 2
    SQL Server is a poor tool for this, if I am honest. You would be far better off trying to use some kind of tool specifically for address lookups that provides the data in a granular enough format for your needs. In some countries, the building/house number can also include letters, so simply assuming that the number is a street if it has a letter in it isn't sufficient. For example you might have "61a Long Road" or "17c Little Avenue". – Thom A Sep 13 '22 at 15:29
  • Parsing an address is a slippery slope. Take a peek at https://stackoverflow.com/questions/41249742/address-standardization-within-a-database/41249971#41249971 – John Cappelletti Sep 13 '22 at 15:40
  • While asking a question, you need to provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example): (1) DDL and sample data population, i.e. CREATE table(s) plus INSERT T-SQL statements. (2) What you need to do, i.e. logic and your code attempt implementation of it in T-SQL. (3) Desired output, based on the sample data in the #1 above. (4) Your SQL Server version (SELECT @@version;). – Yitzhak Khabinsky Sep 13 '22 at 15:45
  • 1
    Also see [Falsehoods programmers believe about addresses](https://www.mjt.me.uk/posts/falsehoods-programmers-believe-about-addresses/) – Thom A Sep 13 '22 at 15:47

0 Answers0