更高效的字符串操作

时间:2016-02-23 10:55:52

标签: sql-server tsql

我不熟悉字符串操作,我想知道是否有更有效的方法来实现我从下面的字符串(来自名为[Details]的列)得到的结果:

 'Probability changed from: [Blank], to: Low. Est Settlement Date changed from: [Blank], to: 2012.'

所以我只想要2012年的部分,我使用以下代码得到它:

ltrim(rtrim(replace(replace(rIGHT(details,LEN(details)-CHARINDEX('to: ',details,Charindex('Settlement Date changed from: [Blank],',details,0))),':',''),'.',''))) as t

这并不重要,因为我得到了我需要的结果但是当我尝试掌握字符串操作时,我会有兴趣看到有适当大脑的人如何做到这一点!

2 个答案:

答案 0 :(得分:1)

试试这个

DECLARE @str NVARCHAR(MAX)='Probability changed from: [Blank], to: Low. Est Settlement Date changed from: [Blank], to: 2012.'
SELECT LEFT(RIGHT(@str,5),4)

答案 1 :(得分:0)

如果你只想要2012年,这还不够吗?

SUBSTRING([Details], LEN([Details]) - 5, 4)