Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-11-24
9
Medium Priority
?
5,469 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 1400 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 600 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 71

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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
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 71

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

718 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