简体   繁体   English

搜索 SQL 服务器表列,只有多个前导和尾随空格

[英]Search SQL Server table column which has multiple leading and trailing spaces only

I am selecting data from a SQL Server table which has leading space characters (could be one, two or more) and also has trailing spaces (could be one, two, and more) also.我正在从 SQL 服务器表中选择数据,该表具有前导空格字符(可能是一个、两个或更多),也有尾随空格(可能是一个、两个和更多)。

I have used LIKE ' %' to search leading spaces value and used LIKE '% ' for trailing spaces records, but the search result displayed only returns with records which has only one space as leading/trailing space.我使用LIKE ' %'搜索前导空格值,并使用LIKE '% '作为尾随空格记录,但显示的搜索结果仅返回只有一个空格作为前导/尾随空格的记录。 But I am not sure the number spaces available in records.但我不确定记录中可用的数字空间。

This is the query that I tried:这是我尝试过的查询:

SELECT [ColumName1], [ColumName], * 
FROM [table1]  
WHERE [ColumName] LIKE ' %' OR [ColumName] LIKE '% '

Expected result:预期结果:

'   Testdata1', 'Test2  ', ' Test3','     Test4 '

But actual result is:但实际结果是:

'Test3 '

There's an easier way to do this, without using likes.有一种更简单的方法可以做到这一点,而无需使用喜欢。 TRIM cuts off trailing whitespaces, you can use that to filter. TRIM切断尾随空格,您可以使用它来过滤。 What you're doing here is comparing the trimmed version of every [ColumName] to the untrimmed version, and returning those that differ.您在这里所做的是将每个[ColumName]的修剪版本与未修剪版本进行比较,并返回不同的版本。 This accomplishes what you want.这完成了你想要的。

select 
[ColumName1], [ColumName], * 
from  [table1]  
WHERE TRIM([ColumName]) <> [ColumName]

This produces all fields that have any (one or more) trailing or leading whitespaces.这将生成具有任何(一个或多个)尾随或前导空格的所有字段。

Depending on your version of SQL Server, TRIM might not be available.根据您的 SQL 服务器版本, TRIM可能不可用。 No matter, there's a workaround around that as well:无论如何,也有解决方法:

select 
[ColumName1], [ColumName], * 
from  [table1]  
WHERE LTRIM([ColumName]) <> [ColumName]
OR RTRIM([ColumName]) <> [ColumName]

You can try this,你可以试试这个

DECLARE @Temp_Table TABLE 
(
    ColumnName VARCHAR(100)
)

INSERT INTO @Temp_Table
VALUES
(' Test1'),
('  Test2'),
('Test3 '),
('Test4  '),
('Test5')

SELECT ColumnName
FROM  @Temp_Table  
WHERE (LEFT(ColumnName,1)=' ' OR RIGHT(ColumnName,1)=' ')

If I am not mistaken, SQL Server's LIKE allows character sets.如果我没记错的话,SQL 服务器的LIKE允许字符集。 We can use this to find control and whitespace characters by excluding characters that we allow (letters and numbers).我们可以通过排除我们允许的字符(字母和数字)来使用它来查找控制和空白字符。 Please try:请试试:

SELECT * 
FROM [table1]  
WHERE [ColumName] LIKE '[^A-Za-z0-9]%' OR [ColumName] LIKE '%[^A-Za-z0-9]';

Try to use LTRIM with RTRIM and LIKE operator to find spaces.尝试将LTRIMRTRIMLIKE运算符一起使用来查找空格。 Let me show an example:让我举个例子:

DECLARE @tbl TABLE 
(
    FooColumn VARCHAR(100)
)

INSERT INTO @tbl
VALUES
(' Example1'),
('  Example2'),
('Example3 '),
('Example4  '),
('Example5')

SELECT 
t.FooColumn
FROM  @tbl t  
WHERE RTRIM(t.FooColumn) LIKE '% %' OR LTRIM(t.FooColumn) LIKE '% %'

Finally i found that column values has been saved with special ascii values, hence those characters were not removed while using LTRIM, RTRIM.最后我发现列值已使用特殊的 ascii 值保存,因此在使用 LTRIM、RTRIM 时不会删除这些字符。

This worked: LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(plaincitation, CHAR(10), CHAR(32)),CHAR(13), CHAR(32)),CHAR(160), CHAR(32)),CHAR(9),CHAR(32))))这工作: LTRIM(RTRIM(REPLACE(REPLACE(REPLACE(REPLACE(plaincitation, CHAR(10), CHAR(32)),CHAR(13), CHAR(32)),CHAR(160), CHAR(32)),字符(9),字符(32))))

Try the following answer.试试下面的答案。

Here I'm inserting the values with spaces and tabs .在这里,我使用spacestabs插入值。 It will give me all the possible results.它会给我所有可能的结果。

Schema:架构:

CREATE TABLE A(ColumnName VARCHAR(100))

INSERT INTO A VALUES(' Test1')
INSERT INTO A VALUES('  Test2')
INSERT INTO A VALUES('Test3 ')
INSERT INTO A VALUES('Test4  ')
INSERT INTO A VALUES('Test5')

T-SQL: T-SQL:

SELECT * 
FROM A
WHERE ColumnName LIKE ' %' OR ColumnName LIKE '%    '
      OR ColumnName LIKE ' %' OR ColumnName LIKE '% '

Check the result in the SQL Fiddle检查SQL 小提琴中的结果

Hope it resolves your issue.希望它能解决你的问题。

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

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