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.
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. 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. What you're doing here is comparing the trimmed version of every [ColumName]
to the untrimmed version, and returning those that differ. 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. 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. 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. 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.
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))))
Try the following answer.
Here I'm inserting the values with spaces
and tabs
. 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:
SELECT *
FROM A
WHERE ColumnName LIKE ' %' OR ColumnName LIKE '% '
OR ColumnName LIKE ' %' OR ColumnName LIKE '% '
Check the result in the SQL Fiddle
Hope it resolves your issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.