Solved

PATINDEX

Posted on 2006-06-27
12
598 Views
Last Modified: 2009-07-29
here's my msgID value:  AAAA061206003028314^74842238^QQQQ

the format of the msgID string is this:
  AAAA - endpoint
  061206 - DDMMYY
  003028314^74842238^QQQQ - ordernumber

i'm using this to strip the UNKNOWN number of zeros from the front of the ordernumber:  
(i want this:  003028314^74842238^QQQQ  to be this:  3028314^74842238^QQQQ )

LEFT(SUBSTRING(msgID,1,10)+SUBSTRING(msgID,11+PATINDEX('%[1-9]%',SUBSTRING(msgID,11,LEN(msgID)-10))-1,LEN(msgID) - 11),
LEN(SUBSTRING(msgID,1,10)+SUBSTRING(msgID,11+PATINDEX('%[1-9]%',SUBSTRING(msgID,11,LEN(msgID)-10))-1,LEN(msgID) - 11)) -2)

stupid oversight of mine, i am sure, but why am i getting this:  
AAAA0612063028314^74842238^QQ
instead of this:
AAAA0612063028314^74842238^QQQQ


0
Comment
Question by:dbaSQL
  • 5
  • 4
  • 3
12 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
Comment Utility
because of the -2?

wouldn't this be "better"

convert(varchar(10),convert(int,substring(msgid,11, charindex('^',msgid,11) - 11)))
+substring(msgid,charindex('^',msgid,11),len(msgid)- charindex('^',msgid,11) + 1)
0
 
LVL 17

Author Comment

by:dbaSQL
Comment Utility
but lowfat, this is my value:  AAAA061206003028314^74842238^QQQQ
your suggestion gives me this:  3028314^74842238^QQQQ      
when i need this:   AAAA0612063028314^74842238^QQQQ

in this particular example, i only need to pull those two 00's after AAAA061206 but before 3028314^74842238^QQQQ
see, there's an unknown number of zeros here.  2, 3, 4...it varies
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 250 total points
Comment Utility
substring(msgid,1,10)+
convert(varchar(10),convert(int,substring(msgid,11, charindex('^',msgid,11) - 11)))
+substring(msgid,charindex('^',msgid,11),len(msgid)- charindex('^',msgid,11) + 1)
0
 
LVL 17

Author Comment

by:dbaSQL
Comment Utility
perfect.  (and way less typing than mine)
but lowfat, i still don't see why mine wasn't cutting it.  do you?
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Or:

LEFT(msgid, 10) + SUBSTRING(msgid, 10 + PATINDEX('%[^0]%', SUBSTRING(msgid, 11, 50)), 50)
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Actually, upon reflection, I think I most prefer this:

STUFF(msgid, 11, PATINDEX('%[^0]%', SUBSTRING(msgid, 11, 50)) - 1, '')
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 17

Author Comment

by:dbaSQL
Comment Utility
why do you prefetr that, scott?  i'm using this in a reconciliation procedure -- performance and runtime is of the essence.  why is one better than the other?
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
I think it will perform better.  It's also (much) shorter, and thus easier to interpret and change, IMO.
0
 
LVL 50

Expert Comment

by:Lowfatspread
Comment Utility
a thought occurs what do yo expect to happen if the first number is actually zero...?
0
 
LVL 17

Author Comment

by:dbaSQL
Comment Utility

  003028314^74842238^QQQQ - ordernumber

Lowfat, if you mean the first number in the ordernumber is actually a zero, then I just think we're kinda outta luck on that one, for sure.  I actually made the very same statement to the parties that be, but I was dismissed.  There is no guarantee that a legitimate ordernumber won't begin with a zero -- but, at this point, I simply haven't accounted for that.  I think, if that actually does occur, we may be invalidating the ordernumber by stripping the legitimate zeros.  As far as my reconciliation goes, it shouldn't hinder me, as I am running this on both sides of the recon -- so, the string still matches.  But, for obvious reasons, we don't want to invalidate the ordernumbers --- so, I believe I just need to emphasize this just a bit more to a few people, and see what's what.

Is that what you meant?

and scott, it actually does perform better -- runtime, i mean.  a teensy bit faster.  why is that?
0
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
Lots fewer functions and no string concatenation; SQL is slow at string concat.
0
 
LVL 17

Author Comment

by:dbaSQL
Comment Utility
yes, definitely a bit slower w/the concatenation.  ok, thank you scott
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

762 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