views:

180

answers:

4

Hi,

I was asked in an interview,a question from oracle sql.this seemed to be a simple question but i had no clue to answer.could anybody help?

if there is string like "newyork is a beautiful city" in a colum.

select column_name from table_name;

will result

newyork is a beautiful city

what is the query required to give the output as a string with all the first letters. i.e., the output should be

niabc
+1  A: 

Maybe it would involve using the function substr?

AJ
Any reason this is not valid?
AJ
substr will be used only if you know the position of the character u need. but you donot know where the word is exactly present. and added to this...this should not be an answer ,rater it should be a comment.so -1 from me
Vijay Sarathi
With a 74% accept rate, I wouldn't be so judgmental of others were I you. You are correct, I only gave a partial answer; I thought it would be enough to get you thinking in the right direction. I guess it wasn't.
AJ
You got my upvote, @AJ :-) I've never used Oracle, but from a quick search it seems like either `substr` or `regex_substr` would work and either one is just as valid as the other, and those seem to be the only real choices for solving the problem. Maybe an Oracle guru will prove us all wrong by telling us about the `get_first_letter_of_every_word_please` function ;-)
Mike Spross
@Mike, thanks and actually that's a valid answer: define your own function in Oracle.
AJ
@AJ,and @Mike...this is not at all a valid answer....as you can see the accepted answer.
Vijay Sarathi
A 74% accept rate is quite good.
Michael Myers
@mmeyers - I assume you mean that as sarcasm...
AJ
No, I don't. I consider anything over 50-60% to be good, and I suspect the average is quite a bit lower than that. Anyway, I tend to agree with devinb: http://meta.stackoverflow.com/questions/23321/is-it-appropriate-to-comment-on-peoples-accept-rate/23326#23326
Michael Myers
@mmeyers - interesting perspective (the meta thread). Actually, if you look at the threads I've responded to, the majority of them *are* n00bs. That's the whole reason I joined the site, to help other developers solve problems. That said, if I'm answering their questions and helping them solve their problems, is it *bad* for me to also encourage them to follow through on their goal of an eventual answer to every question they ask?
AJ
@AJ: Firstly, I don't get notified when you call me "mmeyers". :) Secondly, no, it's certainly not a bad thing. It just seemed unnecessarily critical of someone who's actually participating well. No worries, especially since he seems to have moved on from this question a while ago.
Michael Myers
@mmyers - wow...just realized I'm totally misspelling your name. sorry...!
AJ
@mmyers - you get notified when people _do_ spell your name correctly? How does that happen?
CPerkins
@CPerkins: It's a new feature. You should be notified of this comment because I included @username.
Michael Myers
@mmyers: So, does @Mike Spross get notified now that I used his full screen name, or does the internal space make that impossible? I'm guessing @Mike (or @Alan) won't do the trick. EDIT (sort of): never mind; I see from the relevant blog post that abbreviated names *are* supported (in fact, they pretty much have to be): http://blog.stackoverflow.com/2010/01/new-improved-comments-with-reply/
Alan Moore
@mmeyers: very cool. Thanks.
CPerkins
@Alan: Yep. Unfortunately, "mmeyers" only matches the first two characters so it doesn't notify me.
Michael Myers
@Alan: interestingly enough, I wasn't notified of the new activity going on here in the comment thread, so I'm not sure what's going on there.
Mike Spross
@Alan: Oops...never mind. From the blog: _Only one person can be replied to at a time in a comment. The first one in the string wins._ So @mmyers got notified even though you mentioned me in your comment.
Mike Spross
@Mike: yeah, I missed that, too.
Alan Moore
+5  A: 

The answer is using REGEX_SUBSTR.

See the docs here, and a close example here.

Am
+1 Nice, new to me. Thanks for tip.
AJ
+2  A: 

You could use the split function described here (replacing the comma by a space), in order to split the sentence by its spaces. Then, you could use the substr function as AJ says, because the result of the split would allow you to start from char 1 to char 2 of every "piece".

It involves substr after all, right??

PS. I would rather process the result in a layer above, not during the query. But that's me.

Alfabravo
+5  A: 

Provided you're not concerned with maintaining the case of the output this can be done quite simply without the need for recursion:

SQL> select
  2      translate(
  3            initcap('newyork is a BEAUTIFUL city')
  4               , 'ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz'
  5               , 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  6              )
  7  from dual
  8  /

TRANS
-----
NIABC

SQL>

If the sentence contains numerals, punctuation, etc then we would have to add those characters to the first replacement string, which could get rather tedious.

APC
this is really excellent answer and this is what i was serching for ...a simple sql query:)thanks a lot.
Vijay Sarathi