我正在使用SQL Server 2016:
select
a.ID, v.Comments,
left(v.rest, patindex('%[^$0-9]%', v.rest + ' ') - 1) as amount
from
Table a
cross apply
(values (rtrim(left(Comments charindex('$', Comments-1 )),
stuff(Comments, 1, charindex('$', Comments) -1, ''))) v (Comments, rest)
where
ID v.Comments = 'AAAAAAAAAAA MAXIMUM LIABILITY $67,650 NO LONGER QUA'
我收到一个错误
传递给Left或子字符串函数的Length参数无效。
我的目标是拆分字符串和数值,如
AAAAAAAAAAA MAXIMUM LIABILITY 67,650
以下是我的表的Comments
列中的数据示例
'AAAAAAAAAAA MAXIMUM LIABILITY $67,650 NO LONGER QUA'
'BBBBBBBBBBB MAXI LILIABILITY $$44,77.00-LAND
'CCCCCCCCCCC MAXIU LIABILITY $12,44.90"
您在交叉应用部分发布的查询中出现了一些拼写错误。此外,我在patidix中添加了一个逗号(,(,使其包含所有价格。
select
a.ID, v.Comments,
left(v.rest, patindex('%[^$0-9,]%', v.rest + ' ') - 1) as amount
from a
cross apply
(values (rtrim(left(Comments, charindex('$', Comments)-1 )),
stuff(Comments, 1, charindex('$', Comments) -1, ''))) v (Comments, rest)
如果你愿意,你也可以添加你的位置,但要正确。
where a.Comments = 'AAAAAAAAAAA MAXIMUM LIABILITY $67,650 NO LONGER QUA'
这里有一个sqlfiddle向您展示它是如何工作的