我在修剪它们之后不断地从表中选择列,如下所示:
SELECT TOP 1 RTRIM(LTRIM([UN_DataIN])) FROM [Names]
这将返回名称Fadi
SELECT TOP 1 RTRIM(LTRIM([UN_DataIN])), LEN(RTRIM(LTRIM([UN_DataIN]))) FROM [Names]
当我选择修剪过的柱子的长度时,我会回到10。
这意味着RTRIM
和LTRIM
无法正常工作。
他们有替代品吗?
答案 0 :(得分:3)
这对我来说也可能是我的问题.. ^ - ^
select rtrim(ltrim(replace(replace(replace(colname,char(9),' '),char(10),' '),char(13),' ')))
from yourtable
来源:http://www.sqlservercentral.com/Forums/Topic288843-8-1.aspx
答案 1 :(得分:0)
我想你的田野文字中有一些新的线条。 RTRIM
和LTRIM
不能很好地处理这些问题。
答案 2 :(得分:0)
创建提供所需输出的函数,如下所示:
CREATE FUNCTION dbo.TRIM(@string VARCHAR(MAX))
RETURNS VARCHAR(MAX)
BEGIN
RETURN LTRIM(RTRIM(Regex_Replace(@string,'\n','')));
END
GO
此处,我们正在使用regex_replace
删除是否检测到任何New Lines
,之后它将应用RTRIM
和LTRIM
现在您可以在查询中调用此TRIM函数:
SELECT TOP 1 dbo.TRIM([UN_DataIN]) FROM [Names];
答案 3 :(得分:0)
UN_DataIN == 0x45062706470631062920292029202920292029202000
所以假设阿拉伯语你的字符串以Unicode段落分隔符 U + 2029 结束,然后是你需要删除的所有单个空格;
select rtrim(replace(UN_DataIN, nchar(0x2029), '')) + '!'
答案 4 :(得分:0)
TRIM
所有SPACE
' s TAB
&ENTER
'
DECLARE @Str VARCHAR(MAX) = '
[ Foo ]
'
DECLARE @NewStr VARCHAR(MAX) = ''
DECLARE @WhiteChars VARCHAR(4) =
CHAR(13) + CHAR(10) -- ENTER
+ CHAR(9) -- TAB
+ ' ' -- SPACE
;WITH Split(Chr, Pos) AS (
SELECT
SUBSTRING(@Str, 1, 1) AS Chr
, 1 AS Pos
UNION ALL
SELECT
SUBSTRING(@Str, Pos, 1) AS Chr
, Pos + 1 AS Pos
FROM Split
WHERE Pos <= LEN(@Str)
)
SELECT @NewStr = @NewStr + Chr
FROM Split
WHERE
Pos >= (
SELECT MIN(Pos)
FROM Split
WHERE CHARINDEX(Chr, @WhiteChars) = 0
)
AND Pos <= (
SELECT MAX(Pos)
FROM Split
WHERE CHARINDEX(Chr, @WhiteChars) = 0
)
SELECT '"' + @NewStr + '"'
CREATE FUNCTION StrTrim(@Str VARCHAR(MAX)) RETURNS VARCHAR(MAX) BEGIN
DECLARE @NewStr VARCHAR(MAX) = NULL
IF (@Str IS NOT NULL) BEGIN
SET @NewStr = ''
DECLARE @WhiteChars VARCHAR(4) =
CHAR(13) + CHAR(10) -- ENTER
+ CHAR(9) -- TAB
+ ' ' -- SPACE
IF (@Str LIKE ('%[' + @WhiteChars + ']%')) BEGIN
;WITH Split(Chr, Pos) AS (
SELECT
SUBSTRING(@Str, 1, 1) AS Chr
, 1 AS Pos
UNION ALL
SELECT
SUBSTRING(@Str, Pos, 1) AS Chr
, Pos + 1 AS Pos
FROM Split
WHERE Pos <= LEN(@Str)
)
SELECT @NewStr = @NewStr + Chr
FROM Split
WHERE
Pos >= (
SELECT MIN(Pos)
FROM Split
WHERE CHARINDEX(Chr, @WhiteChars) = 0
)
AND Pos <= (
SELECT MAX(Pos)
FROM Split
WHERE CHARINDEX(Chr, @WhiteChars) = 0
)
END
END
RETURN @NewStr
END
-- Test
DECLARE @Str VARCHAR(MAX) = '
[ Foo ]
'
SELECT 'Str', '"' + dbo.StrTrim(@Str) + '"'
UNION SELECT 'EMPTY', '"' + dbo.StrTrim('') + '"'
UNION SELECT 'EMTPY', '"' + dbo.StrTrim(' ') + '"'
UNION SELECT 'NULL', '"' + dbo.StrTrim(NULL) + '"'
<强>结果强>
+-------+----------------+
| Test | Result |
+-------+----------------+
| EMPTY | "" |
| EMTPY | "" |
| NULL | NULL |
| Str | "[ Foo ]" |
+-------+----------------+
答案 5 :(得分:0)
我的解决方法,如果末尾有一个空格:
update tableName set PayeeName = PayeeName + '_Serengeti' where right(PayeeName,1) = ' '
update tableName set PayeeName = Replace(PayeeName,' _Serengeti','') where PayeeName like '%_Serengeti%'
如果您有很多空格,您可能需要执行语句,直到所有空格都被删除为止。