Solved

PATINDEX REVERSE not giving results expected

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

746 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

12 Experts available now in Live!

Get 1:1 Help Now