首页 > 解决方案 > 在多个地方提取部分字符串

问题描述

如何提取此字符串的一部分

declare @string varchar(1024)
set @string = 'Total # of bytes             : 128270200832 (119.46GB)'
select Substring (@string, Charindex( ':', @string )+2 , Len(@string))

我只需要后面的数字:并且没有(119.46GB)

标签: sql-server

解决方案


使用replace()

select replace(replace(Substring (@string, Charindex( ':', @string )+2 , Len(@string)), '(', ''), ')', '');

编辑:如果你不想要里面的值,()那么你可以这样做:

select left(col, charindex('(', col) - 1) 
from ( values (Substring (@string, Charindex( ':', @string )+2 , Len(@string))) 
     ) t(col);

推荐阅读