I have data in a single column that I need to sub-string out into a new table. My issue is that sometimes the length of the data in the first column is either 10 or 9 characters, then in the second column it's 9 or 8 characters, so I can't find a uniform way to sub-string out each separated piece of data.
The data looks like this:
1465723732 353812955 2 88903391 N L 2016-06-28 13:48:57 2017-06-30 00:12:43 5
990459128 264834338 1 67093407 Y L 2001-01-01 00:00:00 2016-07-09 08:10:20 20
269660184 91753484 1 23492107 Y L 2000-01-01 00:00:00 2013-04-23 09:10:12 15
When the second column is 8 characters, the first is 9 characters. The pieces of data are only seperated by one space rather than evenly into columns , so substringing takes digits from the next piece of data and so on.
Could anyone suggest a method of doing this? Thanks.
This is not elegant but it will help you shape the data:
WITH testdata(col) AS (
SELECT '1465723732 353812955 2 88903391 N L 2016-06-28 13:48:57 2017-06-30 00:12:43 5' UNION
SELECT '990459128 264834338 1 67093407 Y L 2001-01-01 00:00:00 2016-07-09 08:10:20 20' UNION
SELECT '269660184 91753484 1 23492107 Y L 2000-01-01 00:00:00 2013-04-23 09:10:12 15'
), tempdata(cols) AS (
SELECT CAST('<col>' + REPLACE(col, ' ', '</col><col>') + '</col>' AS XML)
FROM testdata
)
SELECT
cols.value('/col[1]', 'INT') AS col1,
cols.value('/col[2]', 'INT') AS col2,
cols.value('/col[3]', 'INT') AS col3,
cols.value('/col[4]', 'INT') AS col4,
cols.value('/col[5]', 'CHAR(1)') AS col5,
cols.value('/col[6]', 'CHAR(1)') AS col6,
cols.value('/col[7]', 'DATE') AS col7,
cols.value('/col[8]', 'TIME(0)') AS col8,
cols.value('/col[9]', 'DATE') AS col9,
cols.value('/col[10]', 'TIME(0)') AS col10,
cols.value('/col[11]', 'INT') AS col11
FROM tempdata
Output:
col1 | col2 | col3 | col4 | col5 | col6 | col7 | col8 | col9 | col10 | col11
-----------+-----------+------+----------+------+------+------------+----------+------------+----------+------
1465723732 | 353812955 | 2 | 88903391 | N | L | 2016-06-28 | 13:48:57 | 2017-06-30 | 00:12:43 | 5
990459128 | 264834338 | 1 | 67093407 | Y | L | 2001-01-01 | 00:00:00 | 2016-07-09 | 08:10:20 | 20
269660184 | 91753484 | 1 | 23492107 | Y | L | 2000-01-01 | 00:00:00 | 2013-04-23 | 09:10:12 | 15