Solved

SQL query to identify fields with leading or trailing white spaces.

Posted on 2010-11-24
9
4,267 Views
Last Modified: 2012-05-10
I need help to formulate a query that identifies fields with leading or trailing spaces in a column in my SQL Server 2008 database.

I know how to eliminate  spaces with ltrim or rtrim but I am not sure how to identify only  those fields that have spaces
0
Comment
Question by:wademi
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 2

Accepted Solution

by:
Atr3ides earned 350 total points
ID: 34207805
Getting leading spaces:
SELECT * FROM SomeTable WHERE LEFT(Column)=' ';

Getting trailing spaces:
SELECT * FROM SomeTable WHERE RIGHT(Column)=' ';

Getting both:
SELECT * FROM SomeTable WHERE RIGHT(Column)=' ' or  LEFT(Column)=' ';
0
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
ID: 34207806
leading spaces:
WHERE yourfield LIKE ' %'

trailing spaces:
well, if the field is VARCHAR, there shall be no trailing spaces
if the field is CHAR, there will be trailing spaces UNLESS the value (which hence has no trailing spaces) has the actual max length of the column

so, searching for trailing spaces, in short, makes no sense
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 34207822
a3,
There is one case (if you converted the column from char to varchar) where the trailing spaces are retained.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 2

Expert Comment

by:Atr3ides
ID: 34207847
Qlemo,
True. The guy or girl seems though to try to find them, so he/she must have them.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34207929
qlemo/a3,

There are other ways to store trailing spaces, even with varchar.

wademi,

The query at the end will find the lead/trailing spaces.

if OBJECT_ID('tempdb..#t1') is not null
drop table #t1
create table #t1 ( vc varchar(100) )
insert #t1 select left('abc r',4)
insert #t1 select left(' abc',4)
insert #t1 select left(' abc ',5)
insert #t1 select 'a b c'
insert #t1 select ''
insert #t1 select null
insert #t1 select 'justaplainstring'

select 'x' + vc + 'x', LEN(vc) from #t1
where LEN('x' + vc + 'x')-2 <> LEN(ltrim(rtrim(vc)))
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34208169
>There are other ways to store trailing spaces, even with varchar.

I only know of the char(160) instead of the normal space char(32) to store trailing spaces in varchar, except of the migration situation from char to varchar ...

what else do you know?
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 34208242
Check this:
set ansi_padding off
create table tmp (vc varchar(10))

insert into tmp values ('a     ');
select '>' + vc + '<' from tmp;
drop table tmp;

set ansi_padding on
create table tmp (vc varchar(10))

insert into tmp values ('a     ');
select '>' + vc + '<' from tmp;
drop table tmp;

Open in new window

0
 
LVL 7

Expert Comment

by:rmm2001
ID: 34208245
For tlrailing spaces - do what @angellll said but reverse the clause WHERE yourfield LIKE '% '

I have the same issue with "dirty data" and trailing/leading whitespace in some of my fields. I'd write a function to get rid of them instead of LTRIM(RTRIM( -ing everything. In that function you'll also want to include a method to get rid of non ascii characters that may have made their way into your field.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34208289
a3,
See my comment http:#a34207929.

insert #t1 select left('abc r',4)

That is one way.

wademi,

You can also use the simple..

select * from #t1 where vc like ' %' or vc like '% '
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question