String Validation using SQL!

Hi Expert,

How to convert the input string to given O/P? using Oracle Sql,

Table : Name

ENAME_iNPUT
--------------------
kalia
kalia  jag
kalia  jag  jagdish
kalia  jag  jagdish  acharya

Expected O/P-
-------------------
kali
kaja
kajj
kjja

It display according to words if one word then it display 1st 4 character and 2 word then display 2 char from 1st word and 2nd char from 2nd word and so on
Please share the select statement how to achieve this using SQL?

Thanks.
MIHIR KAR#Hadoop #Oracle_DB #UNIX beginnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jtriftsMI and AutomationCommented:
Use instring to find occurrence of spaces, substring to snip out, case statement for these for your cases.
0
sdstuberCommented:
your description of intended output does not match the output you have shown.
0
slightwv (䄆 Netminder) Commented:
Is this a learning exercise or actual production requirement?

What if there are 5, 6, or 20 words?
2
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

sdstuberCommented:
Assuming your printed output is correct and your description is wrong then try something like this...

  SELECT ename_input,
         LISTAGG(SUBSTR(str, 1, CASE WHEN cnt = 1 THEN 4 WHEN cnt = 2 THEN 2 WHEN cnt > 2 AND rn = 1 THEN 2 ELSE 1 END))
             WITHIN GROUP (ORDER BY rn)
    FROM (SELECT ename_input,
                 str,
                 cnt,
                 ROW_NUMBER() OVER(PARTITION BY ename_input ORDER BY rn) rn
            FROM (SELECT ename_input,
                         COLUMN_VALUE str,
                         ROWNUM rn,
                         COUNT(*) OVER (PARTITION BY ename_input) cnt
                    FROM name, TABLE(str2tbl(REGEXP_REPLACE(ename_input, ' +', ' '), ' '))) x)
GROUP BY ename_input;


str2tbl is function you will need to create if you don't already ahve it
you can search for it on google or EE
there are lots of examples
1
slightwv (䄆 Netminder) Commented:
I was thinking the CASE looked off.

4 words doesn't produce expected output:  kajja instead of kjja
0
MIHIR KAR#Hadoop #Oracle_DB  #UNIX beginnerAuthor Commented:
Hi @sdstuber I have created the function "str2tab" and the o/p looks little differ  .

But here the requirement is if there are more than 4 word then it should pick only the 1st letter of each word.

SELECT name_input,
         LISTAGG(SUBSTR(str, 1, CASE WHEN cnt = 1 THEN 4 WHEN cnt = 2 THEN 2 WHEN cnt > 2 AND rn = 1 THEN 2 ELSE 1 END))
             WITHIN GROUP (ORDER BY rn)
    FROM (SELECT name_input,
                 str,
                 cnt,
                 ROW_NUMBER() OVER(PARTITION BY name_input ORDER BY rn) rn
            FROM (SELECT name_input,
                         COLUMN_VALUE str,
                         ROWNUM rn,
                         COUNT(*) OVER (PARTITION BY name_input) cnt
                    FROM string_tab, TABLE(str2tbl(REGEXP_REPLACE(name_input, ' +', ' '), ' '))) x)
GROUP BY name_input;

O/P
-----
       kali
     	kaja
       kajj
       kajja  -- Here it picked 1st 2 letter 

Open in new window


Is this a learning exercise or actual production requirement?

Hi @slightwv this is required for production implementation.


Thanks
0
sdstuberCommented:
>>>>  kajja  -- Here it picked 1st 2 letter

maybe  it didn't, it just looks like it did.
what are the inputs for those outputs?

"kalia  jag  jagdish  acharya" > kajja,  2 letters from the first, 1st letter from each of the next 3 words.  Just because the 3rd word happens to have "ja" at the beginning doesn't mean the code picked 2 letters,  the 4th word begins with "a"

for more than 4 words, do you want the 1st letter of all words, including the first, or 2 from the first and 1 from every word thereafter?

If it's 2 from the 1st and 1 for everything thereafter, what I posted already does that.

If it's 1 from every word, then simply change
WHEN cnt > 2
to
WHEN cnt <=4
0
MIHIR KAR#Hadoop #Oracle_DB  #UNIX beginnerAuthor Commented:
For more than 4 words, do you want the 1st letter of all words, including the first, or 2 from the first and 1 from every word thereafter?

If there are 4 or more than 4 words then it should pick only the 1st letter of the word and so on ...

"kalia jag jagdish acharya"                      --  kjja
"kalia jay jagdish acharya hello"             -- kjjah
"kalia jay jagdish acharya hello how"    -- kjjahh

Open in new window


Thanks.
0
sdstuberCommented:
not quite what I posted above, but similar

  SELECT ename_input,
         LISTAGG(SUBSTR(str, 1, CASE WHEN cnt = 1 THEN 4 WHEN cnt = 2 THEN 2 WHEN cnt = 3 AND rn = 1 THEN 2 ELSE 1 END))
             WITHIN GROUP (ORDER BY rn)
    FROM (SELECT ename_input,
                 str,
                 cnt,
                 ROW_NUMBER() OVER(PARTITION BY ename_input ORDER BY rn) rn
            FROM (SELECT ename_input,
                         COLUMN_VALUE str,
                         ROWNUM rn,
                         COUNT(*) OVER (PARTITION BY ename_input) cnt
                    FROM name, TABLE(str2tbl(REGEXP_REPLACE(ename_input, ' +', ' '), ' '))) x)
GROUP BY ename_input;


kalia                                kali
kalia  jag                           kaja
kalia  jag  jbgdish  acharya         kjja
kalia  jag  jcgdish                  kajj
kalia jag jagdish acharya            kjja
kalia jay jagdish acharya hello      kjjah
kalia jay jagdish acharya hello how  kjjahh

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MIHIR KAR#Hadoop #Oracle_DB  #UNIX beginnerAuthor Commented:
Perfect! Thanks , This is accurate as expected o/p.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.