我的string
总是看起来像
项目代码:CUSTOMER PO#:ORDER#:N2:BUCKET#:OKL-1335-081417-A1 RUN#:OKL-1335-081417-A BUILD#:1注意:
如何从此
中仅选择Bucket Number(OKL-1335-081417-A1)
基本上所有between
'Bucket#:'和'Run'。下面是我尝试过的,但是错误输出“传递给LEFT或SUBSTRING函数的长度参数无效。”:
substring(oe_hdr_notepad.note,charindex('Bucket #:',oe_hdr_notepad.note)+len('Bucket #:'),charindex('RUN',oe_hdr_notepad.note)-charindex('Bucket #:',oe_hdr_notepad.note)-len('Bucket #:'))
答案 0 :(得分:0)
尝试一下:
SELECT SUBSTRING(
MyString
FROM (POSITION('BUCKET #: ' IN MyString) + 10) -- Starting point
FOR ( -- Calculate How many characters to grab
POSITION(' RUN #: ' IN MyString) -
(POSITION('BUCKET #: ' IN MyString) + 10)
)
)
FROM (
SELECT 'PROJECT CODE: CUSTOMER PO #: ORDER #: N2: BUCKET #: OKL-1335-081417-A1 RUN #: OKL-1335-081417-A BUILD #: 1 NOTES:' AS MyString
) src
答案 1 :(得分:0)
我刚在SQL Management studio中运行此查询。
<link href="https://cdn.datatables.net/buttons/1.1.2/css/buttons.dataTables.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.11/js/jquery.dataTables.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css" rel="stylesheet"/>
<script src="https://cdn.datatables.net/buttons/1.1.2/js/dataTables.buttons.min.js"></script>
<link href="https://cdn.datatables.net/select/1.1.2/css/select.dataTables.min.css" rel="stylesheet"/>
<script src="https://cdn.datatables.net/select/1.1.2/js/dataTables.select.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="http://cdn.datatables.net/responsive/2.0.2/js/dataTables.responsive.min.js"></script>
<link href="http://cdn.datatables.net/responsive/2.0.2/css/responsive.dataTables.min.css" rel="stylesheet"/>
<div class="container">
<table cellpadding="0" cellspacing="0" border="0" class="dataTable table table-striped" id="example">
</table>
</div>
我得到了下一个输出:
declare @sql varchar(max)
set @sql = 'PROJECT CODE: CUSTOMER PO #: ORDER #: N2: BUCKET #: OKL-1335-081417-A1 RUN #: OKL-1335-081417-A BUILD #: 1 NOTES:'
select substring(@sql,charindex('Bucket #:',@sql)+len('Bucket #:')+1,charindex('RUN',@sql)-charindex('Bucket #:',@sql)-len('Bucket #:')-1)
您的问题可能出在OKL-1335-081417-A1
(1 row(s) affected)
我建议将其打印出来,以确保它与您发布的字符串相同。
如果字符串的长度始终相同,您可以在oe_hdr_notepad.note
中对第三个参数的长度进行硬编码,如:
SUBSTRING
答案 2 :(得分:0)
只是为了好玩,一点XML
示例强>
Declare @YourTable table (id int,note varchar(max))
Insert Into @YourTable values
(1,'PROJECT CODE: CUSTOMER PO #: ORDER #: N2: BUCKET #: OKL-1335-081417-A1 RUN #: OKL-1335-081417-A BUILD #: 1 NOTES: ')
Select A.ID
,Bucket = convert(xml,'<x>'+replace(replace(note,'RUN #:','</x><x>'),'BUCKET #:','</x><x>')+'</x>').value('/x[2]','varchar(100)')
From @YourTable A
<强>返回强>
ID Bucket
1 OKL-1335-081417-A1