Solved

Find leading spaces in column

Posted on 2013-06-30
8
309 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

630 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