[英]How to trim a string in SQL Server before 2017?
在 SQL Server 2017 中,您可以使用此语法,但不能在早期版本中使用:
SELECT Name = TRIM(Name) FROM dbo.Customer;
SELECT LTRIM(RTRIM(Names)) AS Names FROM Customer
To Trim on the right, use:要在右侧修剪,请使用:
SELECT RTRIM(Names) FROM Customer
To Trim on the left, use:要修剪左侧,请使用:
SELECT LTRIM(Names) FROM Customer
To Trim on the both sides, use:要修剪两侧,请使用:
SELECT LTRIM(RTRIM(Names)) FROM Customer
I assume this is a one-off data scrubbing exercise.我认为这是一次性的数据清理练习。 Once done, ensure you add database constraints to prevent bad data in the future eg
完成后,确保添加数据库约束以防止将来出现错误数据,例如
ALTER TABLE Customer ADD
CONSTRAINT customer_names__whitespace
CHECK (
Names NOT LIKE ' %'
AND Names NOT LIKE '% '
AND Names NOT LIKE '% %'
);
Also consider disallowing other characters (tab, carriage return, line feed, etc) that may cause problems.还要考虑禁止可能导致问题的其他字符(制表符、回车符、换行符等)。
It may also be a good time to split those Names into family_name
, first_name
, etc :)也可能是将这些名称拆分为
family_name
、 first_name
等的好时机:)
SELECT LTRIM(RTRIM(Replace(Replace(Replace(name,' ',' '),CHAR(13), ' '),char(10), ' ')))
from author
“替换”的扩展版本:
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RTRIM(LTRIM(REPLACE("Put in your Field name", ' ',' '))),'''',''), CHAR(9), ''), CHAR(10), ''), CHAR(13), ''), CHAR(160), '') [CorrValue]
in sql server 2008 r2 with ssis expression we have the trim function .在带有 ssis 表达式的 sql server 2008 r2 中,我们有修剪功能。
SQL Server Integration Services (SSIS) is a component of the Microsoft SQL Server database software that can be used to perform a broad range of data migration tasks. SQL Server 集成服务 (SSIS) 是 Microsoft SQL Server 数据库软件的一个组件,可用于执行广泛的数据迁移任务。
you can find the complete description on this link您可以在此链接上找到完整的说明
http://msdn.microsoft.com/en-us/library/ms139947.aspx http://msdn.microsoft.com/en-us/library/ms139947.aspx
but this function have some limitation in itself which are also mentioned by msdn on that page.但是此功能本身有一些限制,msdn 在该页面上也提到了这些限制。 but this is in sql server 2008 r2
但这是在sql server 2008 r2 中
TRIM(" New York ") .The return result is "New York".
To trim any set of characters from the beginning and end of a string, you can do the following code where @TrimPattern defines the characters to be trimmed.要从字符串的开头和结尾修剪任何一组字符,您可以执行以下代码,其中 @TrimPattern 定义要修剪的字符。 In this example, Space, tab, LF and CR characters are being trimmed:
在这个例子中,空格、制表符、LF 和 CR 字符被修剪:
Declare @Test nvarchar(50) = Concat (' ', char(9), char(13), char(10), ' ', 'TEST', ' ', char(9), char(10), char(13),' ', 'Test', ' ', char(9), ' ', char(9), char(13), ' ')声明@Test nvarchar(50) = Concat (' ', char(9), char(13), char(10), ' ', 'TEST', ' ', char(9), char(10), char( 13), ' ', '测试', ' ', char(9), ' ', char(9), char(13), ' ')
DECLARE @TrimPattern nvarchar(max) = '%[^ ' + char(9) + char(13) + char(10) +']%'声明@TrimPattern nvarchar(max) = '%[^ ' + char(9) + char(13) + char(10) +']%'
SELECT SUBSTRING(@Test, PATINDEX(@TrimPattern, @Test), LEN(@Test) - PATINDEX(@TrimPattern, @Test) - PATINDEX(@TrimPattern, LTRIM(REVERSE(@Test))) + 2) SELECT SUBSTRING(@Test, PATINDEX(@TrimPattern, @Test), LEN(@Test) - PATINDEX(@TrimPattern, @Test) - PATINDEX(@TrimPattern, LTRIM(REVERSE(@Test))) + 2)
从客户中选择替换(名称,'','')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.