【发布时间】:2021-06-17 04:30:24
【问题描述】:
我使用的是 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'
我遇到了一个错误
传递给左或子字符串函数的长度参数无效。
我的目标是拆分字符串和数值,例如
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"
【问题讨论】:
-
总是最好在你的 charindex 中添加一个“Fail-Safe”。例如: charindex('$', Comments+'$') 注意 +'$'
-
嗨,约翰。我正在尽我所能,但没有工作。
-
您可以使用
NULLIF(PATINDEX(...), 0)以防找不到匹配项
标签: sql sql-server