Solved

Find leading spaces in column

Posted on 2013-06-30
8
307 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 49

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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 49

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 49

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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