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
Solved

PATINDEX REVERSE not giving results expected

Posted on 2014-09-17
4
135 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:
Scott Pletcher 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:Scott Pletcher
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

856 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