Solved

PATINDEX REVERSE not giving results expected

Posted on 2014-09-17
4
133 Views
Last Modified: 2014-09-19
I am learning how to use PATINDEX and REVERSE together.  In this case it is a work assignment and any help given will be most appreciated.

I am attempting to pull the state out of a character string. There are two issues: 1) The state abbreviation can start in the last  position of the string or five+ positions from the end of the string.   2)  when using patindex in the attached TSQL query I am getting the first two characters of the string as opposed to the last two characters.

I am including the first 10 result records and the query itself and have blanked out all data past the state field intentionally as it is not germane to the question and to retain some anonymity to the data.

Once again, I appreciate any help given.
patinedex.rtf
sample-data.xlsx
0
Comment
Question by:Leogal
  • 2
  • 2
4 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 40328722
SELECT        TOP (10) VMAC, VMACT, VMADR1, VMADR2, VMADR3, VMAPVN, VMATPY, VMBUYR, VMCC01, VMCC02, VMCC03,
                         CASE WHEN VMADR3_start_of_state < 2 THEN VMADR3
                              ELSE LEFT(VMADR3, VMADR3_start_of_state - 1) END AS CITY,
                         CASE WHEN VMADR3_start_of_state < 2 THEN ''
                              ELSE SUBSTRING(VMADR3, VMADR3_start_of_state, 2) END AS STATE,
                                     VMCC04, VMCC05, VMCC06, VMCC07, VMCC08,
                         VMCC09, VMCC10, VMCL, VMCMT, VMCNTC, VMCO, VMCOUN, VMCS, VMCTCD, VMCTRY, VMCURR, VMDBNO, VMDSCD, VMDYPY, VMEDGR, VMEDIC, VMFAXN,
                         VMFED, VMGLCD, VMID, VMLDGR, VMLOC, VMLVSN, VMNAME, VMNO, VMOREX, VMPAC, VMPAD1, VMPAD2, VMPAD3, VMPAEE, VMPCNT, VMPCTO, VMPCTU,
                         VMPFXN, VMPHON, VMPNAM, VMPPHN, VMPZIP, VMRGAF, VMSCC, VMSHPC, VMSHPM, VMSPCK, VMSTC, VMTRCD, VMTRMS, VMTXBL, VMVRDY, VMVSDS,
                         VMVSPY, VMVSTX, VMXCNO, VMZIP, VM1099
FROM <table_name>
CROSS APPLY (
    SELECT PATINDEX('%[ ][A-Z][A-Z] %', VMADR3 + ' ') + 1 AS VMADR3_start_of_state
) AS assign_alias_1
0
 

Author Comment

by:Leogal
ID: 40328831
Scott, that works!  I have not used a cross apply before I will need to study more. Thanks very much for your solution.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40328844
You're welcome!

I made a couple of changes after my initial post, so be sure to refresh to make sure you get the final version.

In this case, the CROSS APPLY just allows an alias (name) to be assigned to the PATINDEX() result, so I don't have to repeat the PATINDEX() multiple times.

But the key to the logic is the '%[ ][A-Z][A-Z] %' pattern.  I've tested and verified that it should pull out state correctly even when state is followed by a zip code (as long as at least one space appears after the state).
0
 

Author Closing Comment

by:Leogal
ID: 40332892
Scott, this really helped me out.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

912 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

14 Experts available now in Live!

Get 1:1 Help Now