shuniar
shuniar

Reputation: 2622

SQL Regex Split Column

I am working on an application which is used to scrape data from an excel spreadsheet and store it into a database. The data is not normalized in excel so the scrape needs to be very flexible. I have recently changed the implementation to be somewhat more generic where before

I was storing the ExcelCellLocation and now have broken it up into ExcelColumn, ExcelRowStart, and ExcelRowEnd.

I am looking for a way to take all of the rows in that table and split up the ExcelCellLocation into the new implementation.

For example:

ExcelCellLocation
F5
AB23

Becomes

ExcelColumn | ExcelRowStart | ExcelRowEnd
F           | 5             | 5
AB          | 23            | 23

The reason for the change is that I am now scraping another spreadsheet in the workbook and the implementation needed to be changed in order to better suit the new spreadsheet while still working for the spreadsheet currently being scraped. The new mappings will look more like this:

ExcelColumn | ExcelRowStart | ExcelRowEnd
BC          | 10            | 150
BC          | 160           | 300

Is there a way to do this with a SQL script that will iterate through each record and make the apropriate changes?

Thanks in advance!

Upvotes: 0

Views: 938

Answers (1)

Mikael Eriksson
Mikael Eriksson

Reputation: 138990

-- Sample data
;with T(ExcelCellLocation) as
(
  select 'F5' union all
  select 'AB23'  
)

-- Query
select  
       left(ExcelCellLocation, patindex('%[0-9]%', ExcelCellLocation)-1) as ExcelColumn,
       stuff(ExcelCellLocation, 1, patindex('%[0-9]%', ExcelCellLocation)-1, '') as RowStart
from T

Result:

ExcelColumn RowStart
----------- -----------
F           5
AB          23

Upvotes: 1

Related Questions