Beruflich Dokumente
Kultur Dokumente
45UsefulOracleQueriesforOracleDevelopers
About viralpatel.net
Join Us
Advertise
Search
VIRALPATEL.NET
Home
Android
Java
Spring
Frameworks
Database
JavaScript
Web
More
45 Useful Oracle
Queries
FOLLOW:
AdvertiseHere
1/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
Date / Time
related queries
1.
Your Email
Viral Patel
Follow
3,842 followers
2.
http://viralpatel.net/blogs/usefuloraclequeries/
2/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
particular month.
RECENT POSTS
3.
4.
SPONSORS
5.
http://viralpatel.net/blogs/usefuloraclequeries/
3/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
6.
7.
OR
SELECTTRUNC(sysdate)TRUNC(e.hire_date)
http://viralpatel.net/blogs/usefuloraclequeries/
4/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
9.
Get number of
seconds passed since
today (since 00:00 hr)
http://viralpatel.net/blogs/usefuloraclequeries/
5/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
SELECT(SYSDATETRUNC(SYSDATE))*24*60*60num_of_sec_since_morning
FROMDUAL;
10.
Get number of
seconds left today (till
23:59:59 hr)
SELECT(TRUNC(SYSDATE+1)SYSDATE)*24*60*60num_of_sec_left
FROMDUAL;
Data dictionary
queries
11.
http://viralpatel.net/blogs/usefuloraclequeries/
6/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
12.
Check if a column
exists in a table
Simple query to check if a
particular column exists in
table. Useful when you tries
to add new column in table
using ALTER TABLE
statement, you might wanna
check if column already exists
before adding one.
SELECTcolumn_nameASFOUND
FROMuser_tab_cols
WHEREtable_name='TABLE_NAME'
13.
http://viralpatel.net/blogs/usefuloraclequeries/
7/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
14.
Getting current
schema
Yet another query to get
current schema name.
SELECTSYS_CONTEXT('userenv'
15.
Changing current
schema
Yet another query to change
the current schema. Useful
when your script is expected
to run under certain user but
is actually executed by other
user. It is always safe to set
the current user to what your
script expects.
ALTERSESSIONSETCURRENT_SCHEMA=new_schema;
Database
administration
queries
16.
Database version
information
Returns the Oracle database
http://viralpatel.net/blogs/usefuloraclequeries/
8/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
version.
SELECT*FROMv$version;
17.
Database default
information
Some system default
information.
SELECTusername,
profile,
default_tablespace,
temporary_tablespace
FROMdba_users;
18.
Database Character
Set information
Display the character set
information of database.
SELECT*FROMnls_database_parameters;
19.
20.
http://viralpatel.net/blogs/usefuloraclequeries/
9/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
CREATEINDEXidx1
ONtab(UPPER(col1));
ANALYZETABLEaCOMPUTESTATISTICS
21.
Resizing Tablespace
without adding
datafile
Yet another DDL query to
resize table space.
ALTERDATABASEDATAFILE'/work/oradata/STARTST/STAR02D.dbf'
22.
Checking autoextend
on/off for Tablespaces
http://viralpatel.net/blogs/usefuloraclequeries/
10/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
(OR)
SELECTtablespace_name,AUTOEXTENSIBLE
23.
Adding datafile to a
tablespace
Query to add datafile in a
tablespace.
ALTERTABLESPACEdata01ADD
SIZE1000MAUTOEXTENDOFF
24.
25.
http://viralpatel.net/blogs/usefuloraclequeries/
11/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
26.
27.
28.
http://viralpatel.net/blogs/usefuloraclequeries/
12/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
p.spid,
sql_textsqltext
FROMv$sqltext_with_newlinest,V$SESSIONs,v$processp
WHEREt.address=s.sql_address
ANDp.addr=s.paddr(+)
ANDt.hash_value=s.sql_hash_value
ORDERBYs.sid,t.piece;
Performance
related queries
29.
30.
http://viralpatel.net/blogs/usefuloraclequeries/
13/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
a.serial#,
b.username,
opnameOPERATION,
targetOBJECT,
TRUNC(elapsed_seconds,5)
TO_CHAR(start_time,
ROUND((sofar/totalwork)*100,2)
FROMv$session_longopsa,v$sessionb
WHEREa.sid=b.sid
ANDb.usernameNOT
ANDtotalwork>0
ORDERBYelapsed_seconds;
31.
V$SESSION.SID AND
V$SESSION.SERIAL# is
database process id
V$PROCESS.SPID is
http://viralpatel.net/blogs/usefuloraclequeries/
14/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
shadow process id on
this database server
V$SESSION.PROCESS is
client PROCESS ID, ON
windows it IS : separated
THE FIRST # IS THE
PROCESS ID ON THE
client AND 2nd one IS
THE THREAD id.
32.
33.
http://viralpatel.net/blogs/usefuloraclequeries/
15/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
ORDERBY3DESC)
WHEREROWNUM<10;
34.
35.
36.
http://viralpatel.net/blogs/usefuloraclequeries/
16/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
SELECTusernameUsuario_Oracle,
FROMv$session
GROUPBYusername
ORDERBYNumero_SesionesDESC
37.
Utility / Math
related queries
38.
Convert number to
words
More info: Converting
number into words in Oracle
SELECTTO_CHAR(TO_DATE(1526,
Output:
onethousandfivehundredtwentysix
39.
http://viralpatel.net/blogs/usefuloraclequeries/
17/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
string FOO_SOMETHING in
all package source. This query
comes handy when you want
to find a particular procedure
or function call from all the
source code.
searchastringfoo_somethinginpackagesourcecode
SELECT*
FROMdba_source
WHEREUPPER(text)LIKE'%FOO_SOMETHING%'
ANDowner='USER_NAME';
40.
Convert Comma
Separated Values into
Table
The query can come quite
handy when you have
comma separated data string
that you need to convert into
table so that you can use
other SQL queries like IN or
NOT IN. Here we are
converting
AA,BB,CC,DD,EE,FF string to
table containing AA, BB, CC
etc. as each row. Once you
have this table you can join it
with other table to quickly do
some useful stuffs.
WITHcsv
http://viralpatel.net/blogs/usefuloraclequeries/
18/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
AS(SELECT'AA,BB,CC,DD,EE,FF'
AScsvdata
FROMDUAL)
SELECTREGEXP_SUBSTR(csv.csvdata,
FROMDUAL,csv
CONNECTBYREGEXP_SUBSTR(csv.csvdata,
41.
(OR)
SELECT*FROMemployees
MINUS
SELECT*
FROMemployees
WHEREROWNUM<(SELECTCOUNT
42.
Row Data
Multiplication in Oracle
This query use some tricky
math functions to multiply
http://viralpatel.net/blogs/usefuloraclequeries/
19/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
43.
Generating Random
Data In Oracle
You might want to generate
some random data to quickly
insert in table for testing.
Below query help you do that.
Read this article for more
details.
More info: Random Data in
Oracle
SELECTLEVELempl_id,
MOD(ROWNUM,50000)dept_id,
TRUNC(DBMS_RANDOM.VALUE(1000,500000),2)salary,
http://viralpatel.net/blogs/usefuloraclequeries/
20/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
DECODE(ROUND(DBMS_RANDOM.VALUE(1,2)),1,
TO_DATE(
ROUND(DBMS_RANDOM.VALUE(1,28))
||''
||ROUND(DBMS_RANDOM.VALUE(1,12))
||''
||ROUND(DBMS_RANDOM.VALUE(1900,2010)),
'DDMMYYYY')
dob,
DBMS_RANDOM.STRING(
FROMDUAL
CONNECTBYLEVEL<10000;
44.
Random number
generator in Oracle
Plain old random number
generator in Oracle. This ones
generate a random number
between 0 and 100. Change
the multiplier to number that
you want to set limit for.
generaterandomnumberbetween0and100
SELECTROUND(DBMS_RANDOM.VALUE()*100)+1
45.
http://viralpatel.net/blogs/usefuloraclequeries/
21/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
Related Articles
1. How To Convert Number into
Words using Oracle SQL
Query
2. Generating Random Data in
Oracle
3. Fetch Random rows from
Database (MySQL, Oracle,
MS SQL, PostgreSQL)
4. Row Data Multiplication in
Oracle
5. Oracle Skip Locked
6. Invisible Indexes in Oracle
11g
7. Oracle XMLTable Tutorial
with Example
http://viralpatel.net/blogs/usefuloraclequeries/
22/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
Tags:
Oracle
PL-SQL
SQL
PREVIOUS
NEXT
STORY
STORY
JavaScript
jQuery
Singleton
window
Design
height is
Pattern
not
correct
Row
Data
Mult
iplic
ation
in
Orac
le
Inde
x
Skip
Scan
in
Orac
le
Orac
le
XML
Tabl
e
Tuto
rial
with
http://viralpatel.net/blogs/usefuloraclequeries/
23/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
Exa
mple
How
To
Conv
ert
Num
ber
into
Wor
ds
usin
g
Orac
le
SQL
Quer
y
31 COMMENTS
Madiraju Krishna Chaitanya
21 January, 2014, 7:31
Praveen
21 January, 2014, 14:38
http://viralpatel.net/blogs/usefuloraclequeries/
24/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
Tushar
21 January, 2014, 18:05
Excellent compilation of
queries Viral!.
You rock bhai :)
Reply
krunal
20 February, 2014, 16:01
Thank you
using above database queries
i will solve db related
problems in my many project.
Reply
Harshita
18 March, 2014, 17:24
junaid ahmad
12 April, 2014, 11:34
Nishant Mishra
16 April, 2014, 16:34
http://viralpatel.net/blogs/usefuloraclequeries/
25/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
Use USER_Views
Reply
Nishant Mishra
16 April, 2014, 16:34
Kevin
15 May, 2014, 18:57
26/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
selectdd,next_day(sysdate(rownum*7),dd)DATE_
from(SELECT'MON'
connectbyrownum<=round((sysdateto_date(
union
selectdd,next_day(sysdate(rownum*7),dd)DATE_
from(SELECT'TUE'
connectbyrownum<=round((sysdateto_date(
union
selectdd,next_day(sysdate(rownum*7),dd)DATE_
from(SELECT'WED'
connectbyrownum<=round((sysdateto_date(
union
selectdd,next_day(sysdate(rownum*7),dd)DATE_
from(SELECT'THU'
connectbyrownum<=round((sysdateto_date(
union
selectdd,next_day(sysdate(rownum*7),dd)DATE_
from(SELECT'FRI'
connectbyrownum<=round((sysdateto_date(
orderbydate_
;
Reply
Kevin
15 May, 2014,
http://viralpatel.net/blogs/usefuloraclequeries/
27/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
19:00
Fernando Tlatilolpa
6 May, 2014, 3:59
28/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
by the tags
selectdd,next_day(sysdate(rownum*7),dd)DATE_
from(SELECT'MON'ddfrom
whereto_date('&indate'
andnext_day(sysdate(rownum*7),dd)>=to_date(
connectbyrownum<=round((sysdateto_date(
andnext_day(sysdate(rownum*7),dd)>=to_date(
union
selectdd,next_day(sysdate(rownum*7),dd)DATE_
from(SELECT'TUE'ddfrom
whereto_date('&indate'
andnext_day(sysdate(rownum*7),dd)>=to_date(
connectbyrownum<=round((sysdateto_date(
andnext_day(sysdate(rownum*7),dd)>=to_date(
union
selectdd,next_day(sysdate(rownum*7),dd)DATE_
from(SELECT'WED'ddfrom
whereto_date('&indate'
andnext_day(sysdate(rownum*7),dd)>=to_date(
connectbyrownum<=round((sysdateto_date(
andnext_day(sysdate(rownum*7),dd)>=to_date(
union
selectdd,next_day(sysdate(rownum*7),dd)DATE_
from(SELECT'THU'ddfrom
whereto_date('&indate'
andnext_day(sysdate(rownum*7),dd)>=to_date(
connectbyrownum<=round((sysdateto_date(
andnext_day(sysdate(rownum*7),dd)>=to_date(
union
selectdd,next_day(sysdate(rownum*7),dd)DATE_
from(SELECT'FRI'ddfrom
whereto_date('&indate'
andnext_day(sysdate(rownum*7),dd)>=to_date(
connectbyrownum<=round((sysdateto_date(
andnext_day(sysdate(rownum*7),dd)>=to_date(
orderbydate_;
http://viralpatel.net/blogs/usefuloraclequeries/
29/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
declare
v_datechosendate:=to_date(
v_weeksdiffnumber:=round((sysdatev_datechosen)/7);
v_sysdatedate:=sysdate;
v_ddchar(5);
v_datedate;
cursordatesis
selectdd,next_day(v_sysdate(rownum*7),dd)DATE_
from(SELECT'MON'
wherev_datechosen<=v_sysdate
andnext_day(v_sysdate(rownum*7),dd)>=v_datechosen
connectbyrownum<=v_weeksdiff
next_day(v_sysdate(rownum*7),dd)>=v_datechosen
union
selectdd,next_day(v_sysdate(rownum*7),dd)DATE_
from(SELECT'TUE'
wherev_datechosen<=v_sysdate
andnext_day(v_sysdate(rownum*7),dd)>=v_datechosen
connectbyrownum<=v_weeksdiff
next_day(v_sysdate(rownum*7),dd)>=v_datechosen
union
selectdd,next_day(v_sysdate(rownum*7),dd)DATE_
from(SELECT'WED'
wherev_datechosen<=v_sysdate
andnext_day(v_sysdate(rownum*7),dd)>=v_datechosen
connectbyrownum<=v_weeksdiff
next_day(v_sysdate(rownum*7),dd)>=v_datechosen
union
selectdd,next_day(v_sysdate(rownum*7),dd)DATE_
from(SELECT'THU'
wherev_datechosen<=v_sysdate
andnext_day(v_sysdate(rownum*7),dd)>=v_datechosen
connectbyrownum<=v_weeksdiff
http://viralpatel.net/blogs/usefuloraclequeries/
30/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
next_day(v_sysdate(rownum*7),dd)>=v_datechosen
union
selectdd,next_day(v_sysdate(rownum*7),dd)DATE_
from(SELECT'FRI'
wherev_datechosen<=v_sysdate
andnext_day(v_sysdate(rownum*7),dd)>=v_datechosen
connectbyrownum<=v_weeksdiff
next_day(v_sysdate(rownum*7),dd)>=v_datechosen
orderbydate_;
begin
dbms_output.enable(1000000);
dbms_output.put_line(v_weeksdiff||
opendates;
LOOP
FETCHdatesinto
EXITWHENdates%NOTFOUND;
dbms_output.put_line(v_dd||
ENDLOOP;
closedates;
end;
Reply
31/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
add_months(trunc(sysdate,'MONTH'),csv.Mnt),
add_months(trunc(sysdate,'MONTH'),csv.Mnt)-1 from dual ,csv
Reply
MVVSSARMA
8 July, 2014, 17:33
Dayanandsm
10 July, 2014, 19:40
Malshan
Hi,
http://viralpatel.net/blogs/usefuloraclequeries/
32/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
Makal
3 September, 2014, 20:51
rk goud
6 November, 2014, 18:47
33/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
stufff
Reply
Pugazhendhi_r
17 December, 2014, 7:09
Arun
24 December, 2014, 10:18
Hi,
How to find a value in a pipe
delimiter value.
For ex: Assume that we have a
column A and B. In A column i
have value A1 and in B
column i have a value like
A1|B1|C1|D1. Now i have to
search A1 in B column. Please
note that: A and B columns
are in different table.
Reply
34/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
a2.cust_long_name_717
From Customer a2,
alias_connector where (
connection_alias_778 like %
and (connection_status_778
=BA) and
connection_cust_no_778 =
a2.cust_no_717))
Reply
pratik thakar
19 January, 2015, 12:19
krishna
11 February, 2015, 6:06
i have tables
CREATE TABLE Classes (
className VARCHAR2(20),
typeClass CHAR(2),
country VARCHAR2(15),
numGuns INT,
bore INT,
displacement INT,
CONSTRAINT pkClasses
PRIMARY KEY (className),
CHECK (typeClass IN (bb,
bc))
);
CREATE TABLE Ships
(
shipName VARCHAR2(20),
shipClass VARCHAR2(20),
launchYr INT,
CONSTRAINT pkShips
PRIMARY KEY (shipName),
http://viralpatel.net/blogs/usefuloraclequeries/
35/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
CONSTRAINT fkClasses
FOREIGN KEY (shipClass)
REFERENCES Classes
(className)
);
i want Find for each class, the
year when the latest ship of
that class was launched. Order
the results by the name of the
class.
Reply
Sathishkumar
27 March, 2015, 21:28
36/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
user_info
WHERE action_view.id
=action_path.id
AND action_view.id
=type_view.id
AND
action_view.userid=user_info.user_id
AND action_path.string_value
LIKE
(/app:company_home/st:sites/cm:nextgenedcs/cm:documentLibrary/%)
AND action_view.userid !=
admin
AND type_view.string_value
=cs:ciscodoc
AND action_view.string_value
!= READ
AND action_view.audittime
We are showing audit time
format like mentioned in
green color. The yellow color
condition should be last one
hour from sys time.
Eg:
action_view.audittime >
((sysdate to_date(01-JAN1970,DD-MON-YYYY))-5) *
(86400000)+to_number(to_char(systimestamp,FF3))
but this is not working
properly.
Reply
37/39
08/07/2015
45UsefulOracleQueriesforOracleDevelopers
LEAVE A REPLY
Name *
Email *
Website
Comment
Post Comment
http://viralpatel.net/blogs/usefuloraclequeries/
38/39
08/07/2015
http://viralpatel.net/blogs/usefuloraclequeries/
45UsefulOracleQueriesforOracleDevelopers
39/39