我有一个列,其中包含我需要拆分为三个单独列的信息。我知道我需要使用SUBSTRING功能,但老实说我会如何处理它。有人能提供一些见解吗?以下是返回的当前列:
| Change |
----------------------------------------------------------
| changed status from "new" to "in progress" |
| changed ORT status from "in progress" to "Code Review" |
| changed MileStone from "1.1" to "1.2" |
以下是我需要返回结果的方法:
| ChangeType | ChangeFrom | ChangeTo |
-----------------------------------------------------
| changed status from | new | in progress |
| changed ORT status from | in progress | Code Review |
| changed MileStone from | 1.1 | 1.2 |
提前谢谢!
答案 0 :(得分:1)
这应该有用,假设单词to
和select left(change, chardinex(' from ', change) + 5) as changeType,
replace(substring(change, charindex(' from ', change) + 5,
charindex(' to ', change) - charindex(' from ', change) - 5
), '"', '') as ChangeFrom,
replace(right(change, charindex(' ot ', reverse(change)), '"', '') as changeTo
不会出现在字符串的其他位置:
...
create new application pool
...
sman.CommitChanges();
int i = 0;
const int max = 10;
do
{
i++;
try
{
if (ObjectState.Stopped == pool.State)
{
write_log("Pool was stopped, starting: " + pool.Name);
pool.Start();
}
sman.CommitChanges();
break;
}
catch (System.Runtime.InteropServices.COMException e)
{
if (i < max)
{
write_log("Waiting for IIS to activate new config...");
Thread.Sleep(1000);
}
else
{
throw new Exception(
"CommitChanges timed out efter " + max + " attempts.",
e);
}
}
} while (true);
...
答案 1 :(得分:0)
您可以尝试使用CHARINDEX和SUBSTRING
WITH CTE
AS
(
SELECT change
FROM
(
VALUES ('changed status from "new" to "in progress"'),
('changed ORT status from "in progress" to "Code Review"'),
('changed MileStone from "1.1" to "1.2"')
) A(change)
)
SELECT change,
SUBSTRING(change,q1,q2 -q1 -1),
SUBSTRING(change,q3,q4 -q3 -1)
FROM CTE
CROSS APPLY (SELECT CHARINDEX('"',change,00) + 1) CA1(q1)
CROSS APPLY (SELECT CHARINDEX('"',change,q1) + 1) CA2(q2)
CROSS APPLY (SELECT CHARINDEX('"',change,q2) + 1) CA3(q3)
CROSS APPLY (SELECT CHARINDEX('"',change,q3) + 1) CA4(q4)
答案 2 :(得分:0)
同样有效的解决方案:
SELECT SUBSTRING(n, 1, CHARINDEX('"', n) - 1) AS ChangeType ,
SUBSTRING(n, CHARINDEX('"', n) + 1,
CHARINDEX('"', n, CHARINDEX('"', n) + 1) - CHARINDEX('"', n) - 1) AS ChangeFrom ,
REPLACE(SUBSTRING(n, PATINDEX('% to %', n) + 5, LEN(n)), '"', '') AS ChangeTo
FROM ( VALUES ( 'changed status from "new" to "in progress"' ),
( 'changed ORT status from "in progress" to "Code Review" '),
( 'changed MileStone from "1.1" to "1.2"') ) t ( n )