Wednesday, February 7, 2018

Oracle query to display the string with comma separated value as query row results. With no use of Table creation


There was a situation in my project where query for set of data (result of the query will provide one field with different value)

For example,
Employee table, and fetch only the employee last name.

Now i wanted to test an fix in the application code, where the query was present in the properties file.
Properties file had

query.employee=select distinct employee_firstName from Employee
There was no logical change in code. For testing i need to provide the query with constant values.

For example, for customer name i had data like below

   john1,john2,john3

SELECT REGEXP_SUBSTR (
         'john1,john2,john3',
         '[^,]+',
         1,
         LEVEL)
         val
FROM DUAL
CONNECT BY REGEXP_SUBSTR (
             'jonh1,jonh2,jonh3',
             '[^,]+',
             1,
             LEVEL)
             IS NOT NULL;
The result of the above query will be a Row of values.

john1
john2
john3

Comma separated string transformed to a column of data, without any table.

The RegExp_substr function in oracle has a size limitation if we use more than certain values.
so in that case i was about to use union

SELECT REGEXP_SUBSTR ('1,2,3','[^,]+',1,LEVEL)val FROM DUAL CONNECT BY REGEXP_SUBSTR ( '1,2,3', '[^,]+', 1, LEVEL) IS NOT NULL
union
SELECT REGEXP_SUBSTR ('4,5,6','[^,]+',1,LEVEL)val FROM DUAL CONNECT BY REGEXP_SUBSTR ( '4,5,6', '[^,]+', 1, LEVEL) IS NOT NULL
union
SELECT REGEXP_SUBSTR ('7,8,9','[^,]+',1,LEVEL)val FROM DUAL CONNECT BY REGEXP_SUBSTR ( '7,8,9', '[^,]+', 1, LEVEL) IS NOT NULL
Result of the above query is
1
2
3
4
5
6
7
8
9

There are other ways of achieving this result, in my case i had around 1000 string with comma separated values. For a quick turnaround time the above approach worked.

we can use With query for the same too.









Useful Linux/Unix commands


Below command can be used for a scenario where grep command returns the searching string.

$  grep -H "some text " * | awk -F: '{print $1 }' | wc -l

-H  => as per the Linux man page, displays the file name.
some text => is the text that needs to be searched, double quotes used since there is space between the search string.
* => represents all the files in that folder or where the $ pwd command display value.

-F: => delimits using ":" on the output of grep command.

wc -l => word comm
grep output

file.txt:<some text ....

output of command  < grep -H "some text " * | awk -F: '{print $1 }' >
file.txt

----------------------------------------------

Below command can be used for a scenario where you need to move the files in the directory (Linux) to different location

$ for i in `ls -ltr | grep Jan | awk '{print $9}' `; do mv $i* ../to_delete/; done
Use: ls - lrt command, output is as follows

       -rw-rw-r--. 1 user1 root 2356 Jan  7 12:25 file.xml
       -rw-rw-r--. 1 user1 root 2356 Feb  7 12:25 file.xml

grep Jan => <month> Ls command output format will filter the first record in the above sample.

when delimited with space, the file name is on 9 th position.
awk '{print $9} => provides the file name.

mv $i is the index of the for loop, which is the file name actually.

---------------------------------------------
Below command can be used to print the size of the file in Megabits or Kilobits

$ ls -lr --block-size=M
$ ls -lhS
-h => human readable size format
-S => sort the size

refer man ls page for more details

--------------------------------------------

Tuesday, January 16, 2018

difference between java ArrayList<> add and set method

Below is the example of how the .add and .set method of ArrayList works when using the index & value attribute.

add(int index, E element)
Inserts the specified element at the specified position in this list.
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

set(int index, E element)
Replaces the element at the specified position in this list with the specified element.


import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

public class HelloWorld{

     public static void main(String []args){
        System.out.println("Hello World");
        List<String> listStr = new ArrayList<>();
        listStr.add("1 ");
        listStr.add("2 ");
       listStr.add("3 ");
        listStr.add("4 ");
        listStr.add("5 ");
        listStr.add("6 ");
        listStr.add("7 ");
        listStr.add("8 ");
       
        listStr.forEach(System.out::print);
        List<String> listStr1 =  listStr.stream().collect(Collectors.toList());
        System.out.println("\n *** ADD **** ");
        addAtIndex(listStr1, 5, new String("NewIndxVal "));
        listStr1.forEach(System.out::print);
        System.out.println("\n *** SET **** ");
        List<String> listStr2 = listStr;
         listStr2.forEach(System.out::print);
         System.out.println("");
        setAtIndex(listStr2, 5, new String("NewIndxVal "));
        listStr2.forEach(System.out::print);

     }
    
     public static <T> List<T> addAtIndex (List<T> strList, int idx, T value){
        
         strList.add(idx,value);
        
         return strList;
     }
      public static <T> List<T> setAtIndex (List<T> strList, int idx, T value){
        
         strList.set(idx,value);
        
         return strList;
     }

}

output:
Hello World
1 2 3 4 5 6 7 8 
 *** ADD **** 
1 2 3 4 5 NewIndxVal 6 7 8 
 *** SET **** 
1 2 3 4 5 6 7 8 
1 2 3 4 5 NewIndxVal 7 8