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

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