子字符串和charindex不返回我期望的结果

时间:2019-07-02 15:37:37

标签: sql sql-server tsql

我正在尝试仅从注释列中提取名称。但是,使用子字符串和charindex的组合不会返回我想要的东西。

原始数据如下所示:

MAR-05-2018 01:08:16 PM - Joe Willis (Additional comments) text blah blah 
MAR-25-2018 01:25:19 PM - mitch.ward (Additional comments) text blah blah 
MAR-26-2018 01:33:21 PM - Hank William (Additional comments) text blah blah 

这是我的查询:

SELECT
SUBSTRING(Comment,26,CHARINDEX('(',Comment)) 
FROM ext.x_amr_sp

这是我的结果:

Joe Willis (Condition Action Comments
mitch.ward (Condition Action Comments  
Hank William (Condition Action Comments

我想要:

Joe Willis
mitch.ward
Hank William

2 个答案:

答案 0 :(得分:3)

使用LEFT()作为

SELECT *, LEFT(Str, CHARINDEX('(', Str) - 2) --Here -2 t avoid the leading space
FROM (VALUES
('Joe Willis (Condition Action Comments'),
('mitch.ward (Condition Action Comments'),  
('Hank William (Condition Action Comments')
) T(Str)

自从您更新问题并提供其他数据

SELECT *, SUBSTRING(Str, 27, CHARINDEX('(', Str) - 28)
FROM (VALUES
('MAR-05-2018 01:08:16 PM - Joe Willis (Additional comments) text blah blah'),
('MAR-25-2018 01:25:19 PM - mitch.ward (Additional comments) text blah blah'), 
('MAR-26-2018 01:33:21 PM - Hank William (Additional comments) text blah blah')
) T(Str)

这里是db<>fiddle,以查看其工作方式。

答案 1 :(得分:1)

"self.loading.start(10)
AttributeError: 'Status' object has no attribute 'loading'".