2014-04-04 145 views
1

我想选择所有,直到使用“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%' 

这是错误我得到:

消息4104,级别16,状态1,第3行 多部分标识符 “tbldoc.docid” 无法绑定。 消息4104,级别16,状态1,行3 无法绑定多部分标识符“tbldoc.docext”。

+1

为什么你使用子查询来启动? – jean

+0

,因为我还在学习=) – lyosha

回答

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%' 
+0

没有期待如此快的回应,它的工作完美。非常感谢! – lyosha

+0

@ user2938309我们打算取悦。 :)请记住,当您有足够的代表时,将您的问题标记为已回答并注册。祝你好运! – paqogomez

相关问题