Solved

PATINDEX REVERSE not giving results expected

Posted on 2014-09-17
4
134 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

810 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