我在单个列中有数据,我需要将其子串到新表中。我的问题是,有时第一列中的数据长度是10或9个字符,然后在第二列中它是9或8个字符,所以我找不到一个统一的方法来分出每个分离的一个字符串数据
数据如下所示:
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
当第二列是8个字符时,第一列是9个字符。这些数据只是由一个空格分隔而不是均匀分成列,因此子串将从下一个数据中获取数字,依此类推。
有人可以建议这样做的方法吗?感谢。
答案 0 :(得分:2)
这不是很优雅,但它可以帮助您塑造数据:
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
输出:
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