Skip to Main Content

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Venkat.

Asked: September 28, 2016 - 6:48 pm UTC

Last updated: October 01, 2016 - 1:33 am UTC

Version: 11.2.0.4_32

Viewed 1000+ times

You Asked

Hello I need help with the following Scenario.

I have the table

Create table Descr_temp(Company_cd Varchar2(10), Company_Name Varchar2(254));
with its data



Insert into descr_temp values ('123', 'ABC SOLUTIONS INC');
Insert into descr_temp values ('546', 'XYZ GLOBAL TECH SOLUTIONS');
Insert into descr_temp values ('923', 'NOWHERE COMPANY INC LTD');
Insert into descr_temp values ('113', 'QSW SOLUTIONS');

I need output to be the 1 word if there are 2 words. and I need first 2 words from the description if there are more than 2 words.


Desired Output:
Company_Cd Company_Name
123 ABC SOLUTIONS
345 XYZ GLOBAL
899 NOWHERE COMPANY
654 QSW


------------------------------------------------------------------------
Hello I need help with the following Scenario.

There is a table with Company Cd, Company Name and All I need is the first 2 words of from the Company name if it has more than 3 words and 1 word if it has 2 words

Example:
Company Cd Company Name
123 ABC SOLUTIONS INC
345 XYZ GLOBAL TECH SOLUTIONS
899 NOWHERE COMPANY INC LTD
654 QSW SOLUTIONS

Desired Output:
Company Cd Company Name
123 ABC SOLUTIONS
345 XYZ GLOBAL
899 NOWHERE COMPANY
654 QSW

please help


and Connor said...

THank you for the test case !


SQL> drop table Descr_temp purge;

Table dropped.

SQL> Create table Descr_temp(Company_cd Varchar2(10), Company_Name Varchar2(254));

Table created.

SQL>
SQL>
SQL> Insert into descr_temp values ('123', 'ABC SOLUTIONS INC');

1 row created.

SQL> Insert into descr_temp values ('546', 'XYZ GLOBAL TECH SOLUTIONS');

1 row created.

SQL> Insert into descr_temp values ('923', 'NOWHERE COMPANY INC LTD');

1 row created.

SQL> Insert into descr_temp values ('113', 'QSW SOLUTIONS');

1 row created.

SQL> Insert into descr_temp values ('113', 'ONE_WORD_COMPANY');

1 row created.

SQL>
SQL>
SQL> col company_short_name format a40
SQL> select
  2    company_cd,
  3    case
  4      when instr(Company_Name,' ',1,2) > 0 then substr(Company_Name,1,instr(Company_Name,' ',1,2)-1)
  5      when instr(Company_Name,' ',1,1) > 0  then substr(Company_Name,1,instr(Company_Name,' ',1,1)-1)
  6      else Company_Name
  7    end company_short_name
  8  from  Descr_temp;

COMPANY_CD COMPANY_SHORT_NAME
---------- ----------------------------------------
123        ABC SOLUTIONS
546        XYZ GLOBAL
923        NOWHERE COMPANY
113        QSW
113        ONE_WORD_COMPANY

5 rows selected.

SQL>
SQL>
SQL>


Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Can also do this without case

Thomas Brotherton, September 30, 2016 - 2:36 pm UTC

SQL> with descr_temp as (
  2  select '123' company_cd, 'ABC SOLUTIONS INC' company_name
  3  from dual
  4  union all
  5  select '546' company_cd, 'XYZ GLOBAL TECH SOLUTIONS' company_name
  6  from dual
  7  union all
  8  select '923' company_cd, 'NOWHERE COMPANY INC LTD' company_name
  9  from dual
 10  union all
 11  select '113' company_cd, 'QSW SOLUTIONS' company_name
 12  from dual
 13  union all
 14  select '114' company_cd, 'ONE_WORD_COMPANY' company_name
 15  from dual
 16  )
 17  select company_cd,
 18         nvl(substr(company_name, 1,
 19            greatest(instr(company_name, ' ',1,2)-1,
 20                     instr(company_name, ' ')-1)), company_name) company_name
 21  from descr_temp;

COM COMPANY_NAME
--- --------------------
123 ABC SOLUTIONS
546 XYZ GLOBAL
923 NOWHERE COMPANY
113 QSW
114 ONE_WORD_COMPANY

SQL>

Chris Saxon
October 01, 2016 - 1:33 am UTC

nice work