2016-08-23 85 views
0

使用SQL Server 2012我正在下面的基本查询如何用回车行选择特定的行返回

SELECT MacroID 
     ,MacroText 
     ,AuditEventID 
     ,AuditTypeCode 
     ,AuditTimestamp 
    FROM APXFirm.dbo.AdvMacro 

但是它来到我的注意,每个结果行已多次回车包埋,我需要行内的特定行。

下面是结果集在SQL中的外观。

enter image description here

但是,在记事本中粘贴时,它会是这个样子。

[Version] 3.5.1.212 
[Description] TaxLot information for populating Holdings table 
[Portfolios] @master 
[Mode] Management 
[AutoPrint] No 
[Graph] No 
[Sheet] No 
[Attended] No 
[Apply Copies] No 
[Page Number By] Report 
[Number One Page Reports] No 
[Consolidate Composites] Both 
[Frames] Yes 
[Output File] TxLotExt 
[Error File] TxLotExt 
[Report] txlotext.rep Report txlotext.rep 
$prifile 123103 
$_outfile TxLotExt.xml 
gauge 2 1 
prop MB 
frame g1 2 c y y y "Report txlotext.rep" y - y y 0 0 100 100 000000 ffffff 0 1 "" ffffff 000000 360 0 0 6a240a n 
frame g0 2 r n n y "Report txlotext.rep" y - y y 0 0 100 100 000000 ffffff 0 1 "" ffffff 000000 360 0 0 6a240a n 

我需要的是以[报告]开头的行,包括括号,但我需要下面的信息。

txlotext.rep Report txlotext.rep 

我不知道如何查询一行内的行。

+0

是 “[报告] txlotext.rep报告txlotext.rep” 这个文本要 '提取物'? – Moptan

回答

0
SELECT MacroID 
     ,MacroText 
     ,AuditEventID 
     ,AuditTypeCode 
     ,AuditTimestamp 
    ,LTRIM(RTRIM(LEFT(
     RIGHT(MacroText,LEN(MacroText) - CHARINDEX('[Report] ',MacroText) - LEN('[Report] ')), 
     CHARINDEX(CHAR(13),RIGHT(MacroText,LEN(MacroText) - CHARINDEX('[Report] ',MacroText) - LEN('[Report] '))) - 1 
     ))) as DesiredLine 
    FROM APXFirm.dbo.AdvMacro 

使用CHARINDEX(组合),RIGHT(),左(),和/或子()来找到你想要的东西,并削减了绳子。 CHAR(13)回​​车。

0

试试这个

-- create sample table 
use tempdb 

create table dbo.t (MacroID int identity, MacroText varchar(max)) 
go 
-- populate the table 
insert into dbo.t (MacroText) 
values ('[Version] 3.5.1.212 
[Description] TaxLot information for populating Holdings table 
[Portfolios] @master 
[Mode] Management 
[AutoPrint] No 
[Graph] No 
[Sheet] No 
[Attended] No 
[Apply Copies] No 
[Page Number By] Report 
[Number One Page Reports] No 
[Consolidate Composites] Both 
[Frames] Yes 
[Output File] TxLotExt 
[Error File] TxLotExt 
[Report] txlotext.rep Report txlotext.rep 
$prifile 123103 
$_outfile TxLotExt.xml 
gauge 2 1 
prop MB 
frame g1 2 c y y y "Report txlotext.rep" y - y y 0 0 100 100 000000 ffffff 0 1 "" ffffff 000000 360 0 0 6a240a n 
frame g0 2 r n n y "Report txlotext.rep" y - y y 0 0 100 100 000000 ffffff 0 1 "" ffffff 000000 360 0 0 6a240a n 
') 


-- query 
select substring(macrotext, charindex('[Report]', macrotext)+len('[Report]') 
, charindex(char(0x0d), macrotext, charindex('[Report]', macrotext)) - charindex('[Report]', macrotext) - len('[Report]')) 
from dbo.t