Common SQL queries
Purpose
|
SQL Query
|
O/p
|
Remarks
|
Database
Name
|
s elect ora_database_name from dual; |
SQLDB
|
Return
the database name
|
Full
Information of DB
|
select *
from
v$database; |
DbName,creationdate ..
|
Full
information about the database
|
Current
Time
|
select SYSDATE from dual;
|
03-JUN-14
|
|
Single
line comment
|
-- description
|
||
Multiline
comments
|
/* …..
…… */
|
Function
|
SQL Query
|
||||||||||
Length
–
No of characters in a string
|
Returns
the Length (no of charaters ) of 'MALAYALAM' – 9
select LENGTH(ACCT_NAME)
from ACCOUNTS where
ACCT_ID='598934';
|
||||||||||
INITCAP
– First letterer of each words to capital
letter
|
select INITCAP
(ACCT_NAME) from ACCOUNTS where ACCT_ID='598934'
|
||||||||||
CONCAT
– Concatenate
2 values
|
select CONCAT(ACCT_ID || ':', ACCT_NAME)
from ACCOUNTS where ACCT_ID = '8734545';
|
||||||||||
||
( connate operator) Concatenate 2
values
|
|
||||||||||
UPPER
-Convert characters to upper case
|
select * from ACCOUNTS where UPPER(acct_name) like '%VISHNU%'
|
||||||||||
LOWER
-Convert characters to lower case
|
select * from ACCOUNTS where LOWER(acct_name) like '%vishnu%'
|
||||||||||
ASCII
-Convert characters to ASCII value
|
|
||||||||||
INSTR
-To
find the position of the given letter
INSTR(Value, character to find, starting position from where we need to find Chara
position, which occurrence position you need to find)
|
select INSTR(acct_name,'I',8,
2) from accounts where acct_id = '124466'
|
||||||||||
LTRIM
LTRIM(String,Value)
-Trim the given Value from the left side of the String
|
|
||||||||||
RTRIM
RTRIM(String,Value)
-Trim the given Value from the right
side of the String
|
|
||||||||||
REPLACE
REPLACE(String,valuetofind,valuetoreplace)
-Find the valuetofind in
the String and replace with valuetoreplace
|
|
||||||||||
SUBSTR
SUBSTR(String,startingposition,endingposition)
-substring will extract the String from startingposition
to endingposition and return that
extracted String
|
|
||||||||||
TRANSLATE
TRANSLATE(String, series of characters to find, series of characters to replace)
|
In the
above example series of characters to find
- 'abd'
series of characters to replace
- 'efg'
So it will find every ‘a’ from String 'aaabcdddd'
and replace it with ‘e’
Next, ‘b’ with ‘f’ next – ‘d’ with ‘g’
|
No comments:
Post a Comment