提取字符前后的数字
Extract number before and after character
我需要从 sql 中的 'Input' 列的 'TblA' 中提取特定数字。
我有以下示例输入和所需的输出 1 和 2。
Input
Desired Output 1
Desired Output 2
20 x 88 nc.
20
88
100 x 300 nc
100
300
200x 88 nc.
200
88
5x 300 nc
5
300
ol (200x 88nc.)
200
88
ol (100x 300nc)
100
300
90dfa (45x65)
45
65
90dfa (45 x 65)
45
65
5,5 x 30 nc
5,5
30
5.5 x 30 nc
5.5
30
你能帮我看看这段代码吗?
先感谢您。
我过去曾对第 N 个字符使用 Left 和 right 函数。
但我不知道从哪里开始这段代码。
我正在使用 sql 服务器 2019
假设您的字符串中只有一个 'x'
(就像您所有的示例一样),那么以下工作:
select *
from t cross apply
(select stuff(col1, 1, len(col1) - patindex('%[^0-9,.]%', reverse(col1) + ' ') + 1, '') as col1,
left(col2, patindex('%[^0-9,.]%', col2 + ' ') - 1) as col2
from (values (replace(left(t.input, charindex('x', t.input) - 1), ' ', ''),
replace(stuff(t.input, 1, charindex('x', t.input), ''), ' ', '')
)
) v(col1, col2)
) v;
Here 是一个 db<>fiddle.
我需要从 sql 中的 'Input' 列的 'TblA' 中提取特定数字。 我有以下示例输入和所需的输出 1 和 2。
Input | Desired Output 1 | Desired Output 2 |
---|---|---|
20 x 88 nc. | 20 | 88 |
100 x 300 nc | 100 | 300 |
200x 88 nc. | 200 | 88 |
5x 300 nc | 5 | 300 |
ol (200x 88nc.) | 200 | 88 |
ol (100x 300nc) | 100 | 300 |
90dfa (45x65) | 45 | 65 |
90dfa (45 x 65) | 45 | 65 |
5,5 x 30 nc | 5,5 | 30 |
5.5 x 30 nc | 5.5 | 30 |
你能帮我看看这段代码吗? 先感谢您。 我过去曾对第 N 个字符使用 Left 和 right 函数。 但我不知道从哪里开始这段代码。 我正在使用 sql 服务器 2019
假设您的字符串中只有一个 'x'
(就像您所有的示例一样),那么以下工作:
select *
from t cross apply
(select stuff(col1, 1, len(col1) - patindex('%[^0-9,.]%', reverse(col1) + ' ') + 1, '') as col1,
left(col2, patindex('%[^0-9,.]%', col2 + ' ') - 1) as col2
from (values (replace(left(t.input, charindex('x', t.input) - 1), ' ', ''),
replace(stuff(t.input, 1, charindex('x', t.input), ''), ' ', '')
)
) v(col1, col2)
) v;
Here 是一个 db<>fiddle.