Avatar of MIHIR KAR
MIHIR KAR
Flag for India asked on

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.
Oracle DatabaseSQL

Avatar of undefined
Last Comment
MIHIR KAR

8/22/2022 - Mon
jtrifts

Use instring to find occurrence of spaces, substring to snip out, case statement for these for your cases.
Sean Stuber

your description of intended output does not match the output you have shown.
slightwv (䄆 Netminder)

Is this a learning exercise or actual production requirement?

What if there are 5, 6, or 20 words?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Sean Stuber

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
slightwv (䄆 Netminder)

I was thinking the CASE looked off.

4 words doesn't produce expected output:  kajja instead of kjja
MIHIR KAR

ASKER
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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Sean Stuber

>>>>  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
MIHIR KAR

ASKER
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.
ASKER CERTIFIED SOLUTION
Sean Stuber

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
MIHIR KAR

ASKER
Perfect! Thanks , This is accurate as expected o/p.
Your help has saved me hundreds of hours of internet surfing.
fblack61