Solved

Find leading spaces in column

Posted on 2013-06-30
8
305 Views
Last Modified: 2013-07-01
I have a problem, I am trying to find all serial number in my query that have a space as the first character. I have tried the script below but because of padding there are trailing spaces in the serial result  and is returning all serials not just the ones with leading spaces. I am not  concerned about the trailing spacing, just the leading spaces. I even tried RTRIM in the statement same result all rows returned.

SELECT     SSG_Acquisition_All_T.Type, SSG_Acquisition_All_T.Received_From, SSG_Acquisition_All_T.MANUFACTURE, SSG_Acquisition_All_T.Firearm_Model, 
                      SSG_Acquisition_All_T.Firearm_Caliber, SSG_Acquisition_All_T.Item_Number, SSG_Acquisition_All_T.Serial_Number, 
                      SSG_Dispostion_All_T.Serial_Number AS DISP_SN, SSG_Dispostion_All_T.ItemNo AS DIS_ITEM, SSG_Dispostion_All_T.Ship_To_Name
FROM         SSG_Acquisition_All_T RIGHT OUTER JOIN
                      SSG_Dispostion_All_T ON SSG_Acquisition_All_T.Serial_Number = SSG_Dispostion_All_T.Serial_Number
WHERE     (SSG_Acquisition_All_T.Serial_Number IS NULL) AND (CHARINDEX(' ', SSG_Dispostion_All_T.Serial_Number)) >0
ORDER BY DISP_SN

Open in new window


WHERE     (SSG_Acquisition_All_T.Serial_Number IS NULL) AND RTRIM(CHARINDEX(' ', SSG_Dispostion_All_T.Serial_Number)) >0

Open in new window

0
Comment
Question by:skull52
[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
8 Comments
 
LVL 12

Accepted Solution

by:
duttcom earned 400 total points
ID: 39288720
I'm not sure why you have serial number is null included in your where statement. If you were trying to select only serial numbers which start with a space, you could use -

WHERE     LEFT(SSG_Acquisition_All_T.Serial_Number,1) =' '

Open in new window

0
 
LVL 9

Expert Comment

by:edtechdba
ID: 39288724
How about using the substring function to isolate the 1st character in the serial # string?

WHERE SUBSTRING(SSG_Acquisition_All_T.Serial_Number,1,1) = ' '

Open in new window

0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 100 total points
ID: 39288743
seems you are looking for just those disposition records which:
1. don't have a matching acquisition record
2. have a leading space in Serial_Number

for 1. I would swap the table precedence to use a left join to locate the non-matched disposition records.
for 2. Use the left function on: SSG_Dispostion_All_T.Serial_Number
(substring could be used as an alternative) my point here really is the correct table

please try this
SELECT
       SSG_Acquisition_All_T.Type
     , SSG_Acquisition_All_T.Received_From
     , SSG_Acquisition_All_T.MANUFACTURE
     , SSG_Acquisition_All_T.Firearm_Model
     , SSG_Acquisition_All_T.Firearm_Caliber
     , SSG_Acquisition_All_T.Item_Number
     , SSG_Acquisition_All_T.Serial_Number
     , SSG_Dispostion_All_T.Serial_Number AS DISP_SN
     , SSG_Dispostion_All_T.ItemNo AS DIS_ITEM
     , SSG_Dispostion_All_T.Ship_To_Name
FROM SSG_Dispostion_All_T

LEFT JOIN SSG_Acquisition_All_T
       ON SSG_Dispostion_All_T.Serial_Number = SSG_Acquisition_All_T.Serial_Number

WHERE (SSG_Acquisition_All_T.Serial_Number IS NULL)
AND LEFT(SSG_Dispostion_All_T.Serial_Number,1) =' '
     
ORDER BY DISP_SN

Open in new window

{aopologies, 2 edits, bad typos}
0
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
LVL 48

Expert Comment

by:PortletPaul
ID: 39288759
+ note,
as you are locating just non-matching records via line 17
lines 2 through 8 will all be null as a consequence (and thus may be redundant)
0
 
LVL 32

Expert Comment

by:awking00
ID: 39289978
To find records beginning with a space -
WHERE CHARINDEX(' ',SSG_Acquisition_All_T.Serial_Number) = 1
0
 

Author Comment

by:skull52
ID: 39290260
Paul,
Thanks I took your advice on the LEFT OUTER Join and and removing the redundant columns.   and Duttcom the reason I was using (SSG_Acquisition_All_T.Serial_Number IS NULL) is I only wanted results where there were Dispositions with no Acquisitions and where the serial number started with a space and because you were the first to suggest using LEFT I am awarding you the majority of the points and Paul a few for the suggestion of using the Left OUTER. The  (WHERE CHARINDEX(' ',SSG_Acquisition_All_T.Serial_Number) = 1) and (WHERE SUBSTRING(SSG_Acquisition_All_T.Serial_Number,1,1) = ' ') suggestions both returned 0 results.
0
 

Author Closing Comment

by:skull52
ID: 39290266
As always EE  experts have come to the rescue again
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39292055
Cheers, thanks for the points and quick closure. Paul.

btw :) all 3 methods suggested to locate the leading space will work, they are functionally the same. try this
declare @Serial_Number as varchar(20)
set @Serial_Number = ' leadingSpace'

select
  case when left(@Serial_Number,1) = ' '        then 'found by left'      else 'not found' end as methodleft
, case when substring(@Serial_Number,1,1) = ' ' then 'found by substring' else 'not found' end as methodsubstring
, case when charindex(' ',@Serial_Number) = 1   then 'found by charindex' else 'not found' end as methodcharindex

Open in new window

0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

735 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