Solved

PATINDEX REVERSE not giving results expected

Posted on 2014-09-17
4
136 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
[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
  • 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MS SQL Server connect issues 4 39
Upgrading to SQL Server 2015 Express 2 31
Regarding Disk IO 3 43
SQL Convert "vertical" data to "horizontal" data 4 26
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…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

756 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