答案 0 :(得分:1)
这是一个实现,您可以将其用作选择过程:
DECLARE @row VARCHAR(MAX);
SET @row = 'First:87.85 Second:87.88 mtr'
DECLARE @result VARCHAR(MAX);
SET @result =
RTRIM(LTRIM(REPLACE(REPLACE(REPLACE(@row,'mtr',''),'First:',''),'Second:','')))
SELECT
SUBSTRING(@result,0,charindex(' ',@result)) As First,
SUBSTRING(@result,charindex(' ',@result),LEN(@result)) AS Second
这一次只处理一行。
答案 1 :(得分:0)
假设字符串的结构是固定的,一种相当简单的方法是使用具有很多charindex
列的公用表表达式来获取数字的开始和结束位置,然后使用substring
。
首先,创建并填充示例表(请在您将来的问题中为我们保存此步骤)
DECLARE @T AS TABLE
(
col varchar(100)
)
INSERT INTO @T (col) VALUES
('First:87.85 Second:87.85 mtr'),
('First:8 Second:82 mtr'),
('First:85 Second:8 mtr'),
('First:7.5 Second:87 mtr');
CTE:
WITH CTE AS
(
SELECT col As String,
7 As FirstStart,
CHARINDEX(' Second', col) As FirstEnd,
CHARINDEX(' Second', col) + 8 As SecondStart,
CHARINDEX(' ', col, CHARINDEX(' Second', col)+1) As SecondEnd
FROM @T
)
select语句:
SELECT SUBSTRING(String, FirstStart, FirstEnd - FirstStart) As First,
SUBSTRING(String, SecondStart, SecondEnd - SecondStart) As Second
FROM CTE
结果:
First Second
87.85 87.85
8 82
85 8
7.5 87