Solved

how to determine the number of instances of a character in  string in Oracle

Posted on 2014-09-30
8
343 Views
Last Modified: 2014-09-30
I want to determine how many of a certain letter in a string, e.g., MTWRF10,TR34,TBD

these are class periods, so M=Mon, T=Tues, R=Thurs, etc.

I want to determine how many M's, for example. I can't figure out how to do this in Sql, plus PL/SQL is probably better, no ?

I guess it would just be walking character by character and counting up, I suppose. The logic processes a day at a time, so as I process Monday, I would just walk through the string and count my M's (while ignoring TBD, etc.).

So if there is a Sql trick, please advise. If not, and you have a code snippet to address, that would be excellent.

(oracle is such a broad term, but that looks like the only topic that applies, I don't see PL/SQL).

(I also don't see where to assign points anymore . . . is everything now 500 points ???)
0
Comment
Question by:Gadsden Consulting
  • 4
  • 4
8 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 40352653
Single character?

length(string) - length(replace(string,'M'))
0
 

Author Comment

by:Gadsden Consulting
ID: 40352656
yes, single character.

very slick ! (I should have thought of that . . . )
0
 

Author Comment

by:Gadsden Consulting
ID: 40352670
just what the patient needed !

Thx.
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40352693
Just thought of something.

You wanted to ignore TBD but what I posted will include the 'T' when looking for Tuesday.

Simple fix.  Just grab everything up to the first comma and do the length as above.

There are a few ways but try this:
regexp_substr('MTWRF10,TR34,TBD','[^,]+')

If you are dealing with a LOT of data this one might be faster:
substr(:'MTWRF10,TR34,TBD',1,instr(:'MTWRF10,TR34,TBD',',')-1)
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40352696
>>(I also don't see where to assign points anymore . . . is everything now 500 points ???)

Looks like I even missed that change.  I was just informed that everything is now 500 points.
0
 

Author Comment

by:Gadsden Consulting
ID: 40353534
thanks - I used replace TBD with ''

regexp_substr looks good but I don't understand what is going on there . . .

same with the substr / instr . . . not sure there.

I will try both out tomorrow.

yeah on the 500 points - grade inflation !
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 40353597
The magic is [^,]+

This looks for 1 or more characters '+'  that is NOT '^'  in this list '[]' of characters.  In this case only one, a comma ','


>>same with the substr / instr . . . not sure there.

instr returns the position of the first comma.  Then I subtract one position to remove the comma (really unnecessary for the regex), then grabs the substring from position 1 to that number.

>>grade inflation !

Changes at corporate.
0
 

Author Comment

by:Gadsden Consulting
ID: 40353602
ok, thanks for the expln . . . I'll try tomorrow.
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert in derived column 7 39
Creation date for a PDB 5 39
TSQL Query Into Specific XML Format w/ Multiple Groupings 6 33
Error in creating a view. 8 22
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

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