Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-11-24
9
Medium Priority
?
5,841 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 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
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…

886 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