sql-从字符串列中删除破折号
sql-remove dashes from string column
在存储过程中,我有这个字段
LTRIM(ISNULL(O.Column1, ''))
如果值末尾有破折号(-),要将其删除。仅在 start/end.
处存在破折号的情况下
任何建议
编辑:
微软 SQL 服务器 2014 12.0.5546.0
预期输出:
1)input: "abc-abc" //output: "abc-abc"
2)input: "abc-" //output: "abc"
3)input: "abc" //ouput: "abc"
由于没有提到数据库,这里是你如何做的(而不是找到它)
SQL 服务器
Remove the last character in a string in T-SQL?
甲骨文
Remove last character from string in sql plus
PostgreSQL
Postgresql: Remove last char in text-field if the column ends with minus sign
MySQL
Strip last two characters of a column in MySQL
我认为您可能对此处的字符串操作感到困惑。
此处的 CASE
表达式从您的列中获取 LTRIM/RTRIM
结果并检查两端是否有破折号,然后检查两端是否有破折号。如果破折号存在,它会把它们去掉。它不漂亮,在海量数据上表现不佳,但可以满足您的需求。
数据设置:
create table trim (col1 varchar(10));
insert trim (col1)
values
('abc'),
(' abc-'),
('abc- '),
('abc-abc '),
(' -abc'),
('-abc '),
(NULL),
(''),
(' -abc- ');
查询:
select
case
when right(ltrim(rtrim(isnull(col1,''))),1) = '-'
and left(ltrim(rtrim(isnull(col1,''))),1) = '-'
then substring(ltrim(rtrim(isnull(col1,''))),2,len(ltrim(rtrim(isnull(col1,''))))-2)
when right(ltrim(rtrim(isnull(col1,''))),1) = '-'
then left(ltrim(rtrim(isnull(col1,''))), len(ltrim(rtrim(isnull(col1,''))))-1)
when left(ltrim(rtrim(isnull(col1,''))),1) = '-'
then right(ltrim(rtrim(isnull(col1,''))), len(ltrim(rtrim(isnull(col1,''))))-1)
else ltrim(rtrim(isnull(col1,'')))
end as trimmed
from trim;
结果:
+---------+
| trimmed |
+---------+
| abc |
| abc |
| abc |
| abc-abc |
| abc |
| abc |
| |
| |
| abc |
+---------+
您可以使用 LEFT 函数和 SUBSTRING 来实现结果。
SELECT CASE WHEN RIGHT(stringVal,1)= '-' THEN SUBSTRING(stringVal,1,LEN(stringVal)-1)
ELSE stringVal END AS ModifiedString
from
( VALUES ('abc-abc'), ('abc-'),('abc')) as t(stringVal)
+----------------+
| ModifiedString |
+----------------+
| abc-abc |
| abc |
| abc |
+----------------+
在存储过程中,我有这个字段
LTRIM(ISNULL(O.Column1, ''))
如果值末尾有破折号(-),要将其删除。仅在 start/end.
处存在破折号的情况下任何建议
编辑:
微软 SQL 服务器 2014 12.0.5546.0
预期输出:
1)input: "abc-abc" //output: "abc-abc"
2)input: "abc-" //output: "abc"
3)input: "abc" //ouput: "abc"
由于没有提到数据库,这里是你如何做的(而不是找到它)
SQL 服务器
Remove the last character in a string in T-SQL?
甲骨文
Remove last character from string in sql plus
PostgreSQL
Postgresql: Remove last char in text-field if the column ends with minus sign
MySQL
Strip last two characters of a column in MySQL
我认为您可能对此处的字符串操作感到困惑。
此处的 CASE
表达式从您的列中获取 LTRIM/RTRIM
结果并检查两端是否有破折号,然后检查两端是否有破折号。如果破折号存在,它会把它们去掉。它不漂亮,在海量数据上表现不佳,但可以满足您的需求。
数据设置:
create table trim (col1 varchar(10));
insert trim (col1)
values
('abc'),
(' abc-'),
('abc- '),
('abc-abc '),
(' -abc'),
('-abc '),
(NULL),
(''),
(' -abc- ');
查询:
select
case
when right(ltrim(rtrim(isnull(col1,''))),1) = '-'
and left(ltrim(rtrim(isnull(col1,''))),1) = '-'
then substring(ltrim(rtrim(isnull(col1,''))),2,len(ltrim(rtrim(isnull(col1,''))))-2)
when right(ltrim(rtrim(isnull(col1,''))),1) = '-'
then left(ltrim(rtrim(isnull(col1,''))), len(ltrim(rtrim(isnull(col1,''))))-1)
when left(ltrim(rtrim(isnull(col1,''))),1) = '-'
then right(ltrim(rtrim(isnull(col1,''))), len(ltrim(rtrim(isnull(col1,''))))-1)
else ltrim(rtrim(isnull(col1,'')))
end as trimmed
from trim;
结果:
+---------+
| trimmed |
+---------+
| abc |
| abc |
| abc |
| abc-abc |
| abc |
| abc |
| |
| |
| abc |
+---------+
您可以使用 LEFT 函数和 SUBSTRING 来实现结果。
SELECT CASE WHEN RIGHT(stringVal,1)= '-' THEN SUBSTRING(stringVal,1,LEN(stringVal)-1)
ELSE stringVal END AS ModifiedString
from
( VALUES ('abc-abc'), ('abc-'),('abc')) as t(stringVal)
+----------------+
| ModifiedString |
+----------------+
| abc-abc |
| abc |
| abc |
+----------------+