Friday, August 24, 2012

Let's Play with SQL

1> Function  REGEXP_SUBSTR

REGEXP_SUBSTR, a superset of SUBSTR function.
While SUBSTR extracts a string from a specific location in the target, REGEXP_SUBSTR extracts a string that matches a given pattern, specified with a regular expression, from anywhere in the target string.

Recently I have used this function to get a comma separated string into different rows.
like if a table coulmn has value 'Str1,Str2,Str3,Str4' then I was required to get as
Str1
Str2
Str3
Str4

Examples1 : Target string value 'Str1,Str2,Str3,Str4'

  SELECT REGEXP_SUBSTR ('Str1,Str2,Str3,Str4', '[^,]+', 1, LEVEL) Val
    FROM dual 
  CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE('Str1,Str2,Str3,Str4','[^,]*'))+1

Example2 : Table xx_tab has Column xx_col with target string then

  WITH Tab AS (SELECT xx_col Col
                 FROM xx_tab)
  SELECT TRIM(REGEXP_SUBSTR (Col, '[^,]+', 1, LEVEL))
    FROM Tab 
  CONNECT BY LEVEL <= LENGTH(REGEXP_REPLACE(Col,'[^,]*'))+1


2> Current Oracle Application/Database version

DataBase:

SELECT *
  FROM product_component_version;
OR
SELECT *
  FROM v$version;

Application :
SELECT release_name
  FROM fnd_product_groups;

3> Getting Patch Related Details

SELECT *
  FROM ad_applied_patches ;

Note : it is not storing the Database patches info.

BUG Details

SELECT *
  FROM ad_bugs;

PATCH Set Level

SELECT patch_level
             ,fa.application_short_name
            ,application_name
  FROM fnd_product_installations fp
             ,fnd_application fa
            ,fnd_application_tl fat
 WHERE patch_level is not null
     AND fp.application_id = fa.application_id
     AND fp.application_id = fat.application_id
     AND fat.language = 'US'
ORDER BY fat.application_name;

4> Total Time Taken To Complete a Concurrent Request

select trunc(mod((nvl(actual_completion_date,sysdate) - nvl(actual_start_date,sysdate))*24,60),.1)||' Hr     '||trunc(mod((nvl(actual_completion_date,sysdate) - actual_start_date)*24*60,60),1)||' Mins' "Total Time"
         ,actual_start_date
         ,actual_completion_date
         ,phase_code
         ,status_code
  from fnd_concurrent_requests fcr
 where concurrent_program_id = (select concurrent_program_id
                                                    from fnd_concurrent_programs_tl
                                                  where user_concurrent_program_name = :Conc_program_name
                                                        and language = 'US')

5> Getting multiple rows of data into a single row (11g)

LISTAGG function -
------------------
This function is doing grouping and concatenation of multiple rows of data into a single row per group.

Syntax : LISTAGG(<Column_name> , [<delimiter>]) WITHIN GROUP (ORDER BY <Column_name>)  
        >> [] - optional value

Example :

Data in table xx_receipt_tbl:

   receipt_number       invoice_number
   --------------       --------------
   A30958               A0005475
   A30958               A0005730
   A32249               A5013328
   A32249               A5014550
   A32249               A0053997
   A30787               A0004163

Query:
   select receipt_number
         ,listagg(invoice_number,'~') within group (order by invoice_number)  invoice_numbers
     from xx_receipt_tbl
     group by receipt_number

Result:
   receipt_number         invocie_numbers
   --------------         ---------------
   A30958                 A0005475~A0005730
   A32249                 A5013328~A5014550~A0053997
   A30787                 A0004163   

6> Trimming/Removing leading digits/characters

>Frequently used when you have to remove leading zeros

Ex:
select trim(leading 'A' from 'ATrimTest')
  from dual;

select trim(leading 0 from 0099887766)
  from dual;

OR

 select replace(ltrim(replace(0099007766, '0', ' ')) , ' ', '0')
   from dual
  
 select replace(ltrim(replace('ATrAATest', 'A', ' ')) , ' ', 'A')
   from dual

Sunday, August 28, 2011

FNDLOAD for Territory/Language Specific

Sometimes we need to download Ldts for concurrent program/Lookups/Value Sets etc. which are Territory/language specific.
Hmmmm.....you just need to set the specific languge before downloading/uploading.

>> Related Scripts

>>
SELECT DECODE(parameter, 'NLS_LANGUAGE', 'LANGUAGE',
                         'NLS_TERRITORY', 'TERRITORY',
                         'NLS_CHARACTERSET', 'CHARACTER SET') Attributes,
                value
  FROM nls_database_parameters
 WHERE parameter IN ( 'NLS_LANGUAGE', 'NLS_TERRITORY','NLS_CHARACTERSET')

>>
SELECT NLS_LANGUAGE,NLS_TERRITORY,NLS_CODESET
  FROM fnd_languages
 WHERE nls_territory = 'FRANCE'
   AND language_code = 'F'

Setting up National Language Support
---------------------------------------

EXPORT NLS_LANG=<language>_<territory>.<character set>


Example (Downloading Lookup Ldt)
-------------------------------------

EXPORT NLS_LANG=American_America.UTF8  
>> No Need to do this, as it is already there as default, provided your Territory is American, please check with SQL provided above.

FNDLOAD apps/<apps_password> O Y DOWNLOAD $FND_TOP/patch/115/import/aflvmlu.lct <XX_LOOKUP_NAME_US>.ldt FND_LOOKUP_TYPE APPLICATION_SHORT_NAME="<XXX>"
LOOKUP_TYPE="<XX_Lookup name>"

EXPORT NLS_LANG=French_France.WE8MSWIN1252

FNDLOAD apps/<apps_password> O Y DOWNLOAD $FND_TOP/patch/115/import/aflvmlu.lct <XX_LOOKUP_NAME_FRC>.ldt FND_LOOKUP_TYPE APPLICATION_SHORT_NAME="<XXX>" LOOKUP_TYPE="<XX_Lookup name>"

>> Reset to Default
EXPORT NLS_LANG=American_America.UTF8