选择all,直到第一次出现具有多列的字符

时间:2014-04-04 17:05:25

标签: sql sql-server

我尝试使用" Select all until first occurence of a character"中的一个例子,在第一次出现角色之前选择所有内容。但我还需要选择其他列,例如[docid],[docext],这些都来自tbldoc表.... 这非常适合我需要的东西:

select
substring([ErrorMsg], 1, charindex('.',[ErrorMsg]))
from    (
        select  [ErrorMsg]  from tbldoc as col1

        ) as YourData
where   charindex('.',[ErrorMsg]) > 0
and
[ErrorMsg] like '%TrackRevisions%' 

但是当我尝试添加其他列时,我会收到错误,

这是我尝试使用的代码:

select
tbldoc.[docid], tbldoc.[docext],
substring([ErrorMsg], 1, charindex('.',[ErrorMsg]))
from    (
        select  [ErrorMsg]  from tbldoc as col1

        ) as YourData
where   charindex('.',[ErrorMsg]) > 0
and
[ErrorMsg] like '%TrackRevisions%' 

这是我得到的错误:

Msg 4104,Level 16,State 1,Line 3 多部分标识符" tbldoc.docid"无法受约束。 Msg 4104,Level 16,State 1,Line 3 多部分标识符" tbldoc.docext"无法受约束。

1 个答案:

答案 0 :(得分:1)

您收到错误的原因是您的子查询不包含您想要的字段。但是这里不需要子查询。

select
  tbldoc.[docid], 
  tbldoc.[docext],
  substring(tbldoc.[ErrorMsg], 1, charindex('.',tbldoc.[ErrorMsg]))
from   
  tbldoc
where   
  charindex('.',tbldoc.[ErrorMsg]) > 0
  and tbldoc.[ErrorMsg] like '%TrackRevisions%' 
相关问题