Solved

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

Posted on 2010-11-24
9
3,530 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 142

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 68

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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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 142

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 68

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now