Solved

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

Posted on 2010-11-24
9
4,642 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 70

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
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 
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 70

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

751 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