SQL Funcations
Charater Funcation:
Case manipulation functions
1) Upper
Command : select upper(name) from emp;
OutPut:
+-------------+
| upper(name) |
+-------------+
| SAMAD |
| BILAL |
| LAWANG |
| AMIR |
+-------------+
4 rows in set (0.03 sec)
2) Lower
Command: select lower(name) from emp;
+-------------+
| lower(name) |
+-------------+
| samad |
| bilal |
| lawang |
| amir |
+-------------+
4 rows in set (0.00 sec)
3) Initcap : not available in MYSQL but in oracle you can use this funcation in select statment as we
have already use some charater manipulation funcation.
Character manipulation funcation:
4) SUBSTR:
Command: select substr(name,1,2) from emp;
+------------------+
| substr(name,1,2) |
+------------------+
| sa |
| bi |
| la |
| am |
+------------------+
4 rows in set (0.00 sec)
5) Length
Command: select length(name) from emp;
+--------------+
| length(name) |
+--------------+
| 5|
| 5|
| 6|
| 4|
+--------------+
4 rows in set (0.01 sec)
6) INSTR
Command: select instr(name,'s') from emp;
+-----------------+
| instr(name,'s') |
+-----------------+
| 1|
| 0|
| 0|
| 0|
+-----------------+
4 rows in set (0.00 sec)
7) LPAD
Command: select lpad(name,10,'|') from emp;
+-------------------+
| lpad(name,10,'|') |
+-------------------+
| |||||samad |
| |||||bilal |
| ||||lawang |
| ||||||amir |
+-------------------+
4 rows in set (0.00 sec)
8) RPAD
Command: select rpad(name,10,'|') from emp;
+-------------------+
| rpad(name,10,'|') |
+-------------------+
| samad||||| |
| bilal||||| |
| lawang|||| |
| amir|||||| |
+-------------------+
4 rows in set (0.00 sec)
9) TRIM
Commad: select TRIM('m' from 'mom') from dual;
+----------------------+
| TRIM('m' from 'mom') |
+----------------------+
|o |
+----------------------+
1 row in set (0.00 sec)
10) Replace
Command: select replace('am' ,'a', 'mo') from dual;
+--------------------------+
| replace('am' ,'a', 'mo') |
+--------------------------+
| mom |
+--------------------------+
1 row in set (0.00 sec)
11) Sub-Query
select name from emp where salary < (select salary
Command:
from emp where name ='samad');
+--------+
| name |
+--------+
| bilal |
| lawang |
| amir |
+--------+
3 rows in set (0.00 sec)
12) Nested padding
Command :select (Rpad(lpad('amir' ,10, '*'),20,'K');
+------------------------------------+
| Rpad(lpad('amir' ,10, '*'),20,'K') |
+------------------------------------+
| ******amirKKKKKKKKKK |
+------------------------------------+
1 row in set (0.00 sec)
13) Insert new rows
Command: Insert into emp(name, dept,salary) values (“amir”,10,2000);
Command:insert into emp vlaues (“asif”,20,3000);
14) Inserting a null value
Command: insert into emp(“junaid”,10,NULL,NULL);
15) Insert new record using sub-query
Command: insert into emp(name,dept,salary)
select name,id,salary from test;
16) Update all rows
Command: update test set id=10;
17) Update specific rows
Command: update test set id=20 where id=30;
18) Update specific columns using sub-query
Command: update emp
set name=(select name from test where id=20)
where dept=20;
19) Delete all rows
Command: delete from test;
20) Delete specifics rows
Command: delete from emp where dept=10;
21)