简体   繁体   English

如何在 t-sql 中使用 patIndex 删除右括号

[英]How to remove closing parenthesis using patIndex in t-sql

Below is the select I am using to test my patindex.下面是我用来测试我的 patindex 的 select。 I am trying to remove all special characters but the result set that comes back is like below.我正在尝试删除所有特殊字符,但返回的结果集如下所示。 Essentially there are some negative numbers with accounting formatting.本质上,会计格式存在一些负数。 I need to remove this formatting and have it just be numbers ($102.00).我需要删除这种格式,让它只是数字(102.00 美元)。

Sql:
[
0.00,
$102.00)]
select  cast(rtrim(ltrim(Replace([Amount],SUBSTRING([Amount],patindex('%[^0-9]%',[Amount]),1),'') ) )as varchar) from Temp2

Does anyone know how to remove that closing ) ?有谁知道如何删除该关闭) I have tried a bunch of regex but I just cant get it to go away.我已经尝试了一堆正则表达式,但我无法将它传递给 go。

Any help would be greatly appreciated.任何帮助将不胜感激。

Edit: my apologies.编辑:我的道歉。 I am using Sql Server I start with [$0.00,($25.00)] I get the above.我正在使用 Sql 服务器我从[$0.00,($25.00)]开始,我得到了上述结果。 I need it to look like [0.00,25.00]我需要它看起来像[0.00,25.00]

I have tried a patindex of '%[()]%' or this '%[\(\)%]' which only removes the first one.我尝试了'%[()]%'的 patindex 或仅删除第一个'%[\(\)%]' and if I flip it, it only removes the closing parenthesis but not the opening one.如果我翻转它,它只会删除右括号,但不会删除左括号。 And a bunch of other combinations.还有一堆其他的组合。

You can just use Replace 3 times to remove the unwanted characters.您可以使用 Replace 3 次来删除不需要的字符。

Declare @s varchar(100)
Select @s = '[$0.00,($25.00)]'
Select @s as original,  Replace(Replace(Replace(@s,'$',''),'(',''),')','') as cleaned

If you need to apply that to a column (ex. Amount) in a table, instead of @s, use that column name in a SQL query如果您需要将其应用于表中的列(例如金额),而不是 @s,请在 SQL 查询中使用该列名

Select ...., Replace (.... <<Amount>> ) from yourTable

Here is the fiddle这是小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM