Monday, November 21, 2016

Useful Oracle SQL Info

     1.Trim Example

SELECT TRIM (' 7'),
       TRIM (' 777 77  '),
       TRIM (' ' FROM '  7 7 '),
       TRIM (LEADING '0' FROM '000123'),
       TRIM (TRAILING '1' FROM 'Tech1'),
       TRIM (BOTH '1' FROM '123Tech111')

  FROM DUAL







        2. Sql to add seconds, minutes, hours to current date

SELECT SYSDATE,
       SYSDATE + (1 / (24 * 60 * 60)) "1Sec Addition",
       sysdate + interval '1' second,
       SYSDATE + (1 / (24 * 60)) "1Min Addition",
       sysdate + interval '1' minute,
       SYSDATE + (1 / 24) "1HR Addition",
       sysdate + interval '1' hour
  FROM DUAL


3.Converting string into rows

with t as (select 'abcd,123,defoifcd,87765' as str from dual)
    select level as n, regexp_substr(str,'[^,]+',1,level) as val
    from   t
   connect by regexp_substr(str,'[^,]+',1,level) is not null



    4.Converting string into columns

with t as (select 'abcd,123,defoifcd,87765' as str from dual)
    select max(decode(level,1,regexp_substr(str,'[^,]+',1,level))) as val1
          ,max(decode(level,2,regexp_substr(str,'[^,]+',1,level))) as val2
          ,max(decode(level,3,regexp_substr(str,'[^,]+',1,level))) as val3
          ,max(decode(level,4,regexp_substr(str,'[^,]+',1,level))) as val4
          ,max(decode(level,5,regexp_substr(str,'[^,]+',1,level))) as val5
   from   t
  connect by regexp_substr(str,'[^,]+',1,level) is not null


    5.Listagg Test

create table dept_test
(
  dno       number,
  dname     varchar2(100 byte),
  location  varchar2(100 byte)
)

set define off;
insert into dept_test   (dno, dname, location) values   (1, 'Accounts', 'NY');
insert into dept_test   (dno, dname, location) values   (2, 'Delivery', 'NJ');
insert into dept_test   (dno, dname, location) values   (3, 'Customer Support', 'NJ');
insert into dept_test   (dno, dname, location) values   (4, 'Billing', 'NY');
insert into dept_test   (dno, dname, location) values   (5, 'Payments', 'NY');
commit;


select * from  dept_test
select location, listagg (dname,',') within group (order by dname) dept_list
from  dept_test
group by location


    6. Notepad++ add quotes at begin & end of each line in a file



7.Convert String with comma separated values into Columns Using SQL
1.       select * from  test
select  c1, trim(regexp_substr(c2,'[^,]+',1,level)) c2
from   test
connect by regexp_substr(c2,'[^,]+',1,level) is not null
and c2 = prior c2
and c1 = prior c1
and prior sys_guid() is not null

order by c1






2 comments:

  1. Hi mahendra,

    Thanks for your Query .Can u Please explain the below Query how its get executed ?

    select c1, trim(regexp_substr(c2,'[^,]+',1,level)) c2
    from test
    connect by regexp_substr(c2,'[^,]+',1,level) is not null
    and c2 = prior c2
    and c1 = prior c1
    and prior sys_guid() is not null

    order by c1


    2) we can get result by using below Query also
    SELECT DISTINCT C1,REGEXP_SUBSTR(C2,'[^,]+',1,LEVEL) AS SF
    FROM TEST_3_1
    CONNECT BY REGEXP_SUBSTR(C2,'[^,]+',1,LEVEL) IS NOT NULL;
    /

    ReplyDelete
  2. Nice information, valuable and excellent in Job, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.mobile phone repair in Novi
    iphone repair in Novi
    cell phone repair in Novi
    phone repair in Novi
    tablet repair in Novi
    ipad repair in Novi
    mobile phone repair Novi
    iphone repair Novi
    cell phone repair Novi
    phone repair Novi

    ReplyDelete