Tuesday, 17 March 2015

To_char Function in sql, Convert Numbers Into words


1.Create a query that displays the employees ’ last names and commission amounts. If anemployee does not earn commission, show  “No Commission.” Label the column   COMM

Query is as follows

SQL> SELECT last_name,
NVL(TO_CHAR(commission_pct), 'No Commission') COMM
FROM employees;


2. How to convert number into words
Example :-

 SELECT TO_CHAR (TO_DATE (260050, 'j'), 'jsp') FROM DUAL;


SQL> /

TO_CHAR(TO_DATE(260050,'J'),'JSP
--------------------------------
two hundred sixty thousand fifty

Wednesday, 11 March 2015

SQL RENAME Command

SQL RENAME Command

The SQL RENAME command is used to change the name of the table or a database object.

If you change the object's name any reference to the old name will be affected. You have to manually change the old name to the new name in every reference.
Syntax to rename a table

RENAME old_table_name To new_table_name;

For Example: To change the name of the table employees to my_employee, the query would be like

RENAME employees  TO my_emloyee;

Select statement

Select Clause In sql

SQL (Structured Query Language) is used to modify and access data or information from a storage area called database.

This beginner sql tutorial website teaches you the basics of SQL and how to write SQL queries. I will be sharing my

knowledge on SQL and help you learn SQL better. The sql concepts discussed in this tutorial can be applied to most of

database systems. The syntax used to explain the concepts is similar to the one used in Oracle database.

Some Of the most important Sql Commands are as follwes:

SQL SELECT Statement

The most commonly used SQL command is SELECT statement. The SQL SELECT statement is used to query or retrieve data

from a table in the database. A query may retrieve information from specified columns or from all of the columns in the

table. To create a simple SQL SELECT Statement, you must specify the column(s) name and the table name. The whole

query is called SQL SELECT Statement.

Note: SQL is not case sensitive. SELECT is the same as select.

Syntax of SQL SELECT Statement:

SELECT column_list FROM table-name
[WHERE Clause]
[GROUP BY clause]
[HAVING clause]
[ORDER BY clause];

Table-name is the name of the table from which the information is retrieved.
    column_list includes one or more columns from which data is retrieved.

The SQL SELECT Statement

The SELECT statement is used to select data from a database.

The result is stored in a result table, called the result-set.

SELECT * Example

Now we want to select all the columns from the "Employees" table.

SQL> select * from employees;

Now we want to select the content of the columns named

"Employee_id", "LastName" and "FirstName" from the table above.

SQL> select employee_id,first_name,last_name from employees;


Where Clause:-

SQL HAVING Clause

Having clause is used to filter data based on the group functions. This is similar to WHERE condition but is used with group

functions. Group functions cannot be used in WHERE Clause but can be used in HAVING clause.

For Example: If you want to select the department that has total salary paid for its employees more than 25000, the sql

query would be like;

SELECT departmet_id, SUM (salary)
FROM employees
GROUP BY departmets
HAVING SUM(salary) > 25000

When WHERE, GROUP BY and HAVING clauses are used together in a SELECT statement,
the WHERE clause is processed first, then the rows that are returned after the WHERE clause is executed are grouped

based on the GROUP BY clause. Finally, any conditions on the group functions in the HAVING clause are applied to the

grouped rows before the final output is displayed.
SQL ORDER BY

The ORDER BY clause is used in a SELECT statement to sort results either in ascending or descending order. Oracle sorts

query results in ascending order by default.

For Example: If you want to sort the employee table by salary of the employee, the sql query would be.

SELECT first_name, salary FROM employee ORDER BY salary;



The query first sorts the result according to name and then displays it.

You can also use more than one column in the ORDER BY clause.
he query first sorts the result according to name and then displays it.

You can also use more than one column in the ORDER BY clause.                                                                                                          



Alter Table :-
SQL ALTER TABLE Statement

The SQL ALTER TABLE command is used to modify the definition (structure) of a table by modifying the definition of its columns. The ALTER command is

used to perform the following functions.

1) Add, drop, modify table columns
2) Add and drop constraints
3) Enable and Disable constraints

Syntax to add a column

ALTER TABLE table_name ADD column_name datatype;

For Example: To add a column "experience" to the employee table, the query would be like

ALTER TABLE employees  ADD experience number(3);

Syntax to drop a column

ALTER TABLE table_name DROP column_name;

For Example: To drop the column "location" from the employees  table,
the query would be like


ALTER TABLE employees DROP location;

Syntax to modify a column

ALTER TABLE table_name MODIFY column_name datatype;

For Example: To modify the column salary in the employees  table, the query would be like

ALTER TABLE employees MODIFY salary number(15,2);


SQL RENAME Command

The SQL RENAME command is used to change the name of the table or a database object.

If you change the object's name any reference to the old name will be affected. You have to manually change the old name to the new name in every reference.
Syntax to rename a table

RENAME old_table_name To new_table_name;

For Example: To change the name of the table employees to my_employee, the query would be like

RENAME employees  TO my_emloyee;



The SQL SELECT DISTINCT Statement
In a table, some of the columns may contain duplicate values. This is not a problem, however,
 sometimes you will want to list only the different (distinct) values in a table.

The DISTINCT keyword can be used to return only distinct (different) values.

SQL SELECT DISTINCT Syntax
SELECT DISTINCT column_name(s)
FROM table_name

SELECT DISTINCT City FROM emp_info;


SQL WHERE Clause

The WHERE clause is used to extract only those records that fulfill a specified criterion

SELECT * FROM Emp_info
WHERE City='Mumbai'
Use Quotes Around Text Fields
SQL uses single quotes around text values (most database systems will also accept double quotes).

However, numeric values should not be enclosed in quotes....






Display the last name and department number of all employees in departments 20 and 50 in
alphabetical order by name.
SELECT last_name, department_id
FROM employees
WHERE department_id IN (20, 50)

ORDER BY last_name;

Tuesday, 10 March 2015

Substr Function In Sql



Substr: The oracle substr function allows you to extract a substring .

SQL>  select substr('santosh chaurasia',2,6) from dual;

SUBSTR
------
antosh

sql> select substr('priyaseth',4) from dual;
SUBSTR
------
antosh

SQL> select substr('avinashkare',-4) from dual;
SQL> select instr('nikita_sharma','k') from dual;

INSTR('NIKITA_SHARMA','K')
--------------------------
                         3
SQL> select instr('america','e') from dual;
The instr function in sql used to find the starting locatin of a pattern in a string.
The syntax for instr(str) is as follows.
instr(str,pattern,[starting position,[nth locatin]])

INSTR('AMERICA','E')
--------------------
                   3
SQL>  select instr('nikitasony','n',2) from dual;-- for 2nd occurance

INSTR('NIKITASONY','N',2)
-------------------------
                        9
SQL> select lpad('vikram',10,'*') from dual;

LPAD('VIKR
----------
****vikram

SQL> select rpad('nasim',10,'*') from dual;

RPAD('NASI
----------
nasim*****

Count Function SQL

Count Function:-  This function is used to count the number of rows returned in a statement.
Syntax:- The syntax for sql count is:

Select count(expression) from tabls where conditions;
sql > select count(department_id)" No_of_employees"  from employees;

 No_of_employees
----------------
             107

Saturday, 7 March 2015

What is View in sql




How to Create SQL View

Introduce to SQL View

SQL View is a virtual table which is used to encapsulate a complex queries. After creating SQL view, you can treat a view as a table and

manipulate data on it with only some restrictions. When the data in table changes, the data in view which is dependent on table changes also.

View does not occupied the physical space as table does. The syntax of creating view as follows:


CREATE VIEW SALARY_REC
AS
SELECT e.last_name,
e.salary,
d.department_name AS department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.department_id

Output:

SELECT * FROM SALARY_REC


OConnell 2860 Shipping
Grant 2860 Shipping
Whalen 4840 Administration
Hartstein 14300 Marketing
Fay 6600 Marketing
Mavris 7150 Human Resources
Baer 11000 Public Relations
Higgins 13200 Accounting
Gietz 9130 Accounting
Sullivan 2750 Shipping
Geoni 3080 Shipping
Sarchand 4620 Shipping
Bull 4510 Shipping

IN Condition in sql

An In_condition is a membership condition. it tests a value for membership in a list of values or
subquery.

The Condition defined using in Operator is also known as the membership condition.
In works with values of all data types..

When we using character,dates or non alpha -numeric strings,the list items must be enclosed with single quotation marks For example('Delhi','Mumbai') as shown in second example

SQL> select employee_id,job_id,salary
  2  from employees
  3  where employee_id in(100,102,107);

EMPLOYEE_ID JOB_ID         SALARY
----------- ---------- ----------
        100 AD_PRES         16000
        102 AD_VP           17000
        107 IT_PROG          4200

In the query above ,oracle would select only those records where the employee_id equal to either 100,102,107.
Example 2:in the query below ,oracle would select only those records where job_id is equal to 'IT_PROG','SA_REP'.
remember that this items are enclosed with single quotation marks since they are non-numeric-strings.

SQL> select employee_id,last_name,job_id
  2  from employees
  3  where job_id in('IT_PROG','SA_REP');

EMPLOYEE_ID LAST_NAME                 JOB_ID
----------- ------------------------- ----------
        103 Hunold                    IT_PROG
        104 Ernst                     IT_PROG
        105 Austin                    IT_PROG
        106 Pataballa                 IT_PROG
        107 Lorentz                   IT_PROG
        150 Tucker                    SA_REP
        151 Bernstein                 SA_REP
        152 Hall                      SA_REP
        153 Olsen                     SA_REP
        154 Cambrault                 SA_REP
        155 Tuvault                   SA_REP

EMPLOYEE_ID LAST_NAME                 JOB_ID
----------- ------------------------- ----------
        156 King                      SA_REP
        157 Sully                     SA_REP
        158 McEwen                    SA_REP
        159 Smith                     SA_REP
        160 Doran                     SA_REP
        161 Sewall                    SA_REP
        162 Vishney                   SA_REP
        163 Greene                    SA_REP
        164 Marvins                   SA_REP
        165 Lee                       SA_REP
        166 Ande                      SA_REP

EMPLOYEE_ID LAST_NAME                 JOB_ID
----------- ------------------------- ----------
        167 Banda                     SA_REP
        168 Ozer                      SA_REP
        169 Bloom                     SA_REP
        170 Fox                       SA_REP
        171 Smith                     SA_REP
        172 Bates                     SA_REP
        173 Kumar                     SA_REP
        174 Abel                      SA_REP
        175 Hutton                    SA_REP
        176 Taylor                    SA_REP
        177 Livingston                SA_REP

EMPLOYEE_ID LAST_NAME                 JOB_ID
----------- ------------------------- ----------
        178 Grant                     SA_REP
        179 Johnson                   SA_REP

35 rows selected.
















Friday, 6 March 2015

Truncate function in sql

1. Get the last day of the month.

sql> SELECT TRUNC (LAST_DAY (SYSDATE)) "Last day of current month"
    FROM DUAL;


2.  Get the first day of the Year

sql> SELECT TRUNC (SYSDATE, 'YEAR') "Year First Day" FROM DUAL;

Wednesday, 4 March 2015

Conversion Function in Sql


Conversion function

Implicit conversion:-

A varchar2 or char value can be implicitly converted to
Number or date type value by oracle.
Similarly, a number or data type can be automatically
Converted to character data by oracle server.
For example the below select queries .both the queries will
Give the same output because oracle internally treats 10000
And '10000' are same.

Query-1
select employee_id,first_name,last_name,salary
From employees
Where salary > 10000;


Query-2

YYYY Full year in numbers
YEAR Year spelled out (in English)
MM Two-digit value for month
MONTH Full name of the month
MON Three-letter abbreviation of the month
DY Three-letter abbreviation of the day of the week
DAY Full name of the day of the week
DD Numeric day of the month

To_char functions
Element Result
9 Represents a number
0 Forces a zero to be displayed
$ Places a floating dollar sign
L Uses the floating local currency symbol
. Prints a decimal point
, Prints a comma as thousands indicator


select employee_id,first_name,last_name,salary
from employees
where salary > '10000';

Explicit data type conversion are single row function which are capable of

typecasting column value,literal or expression.
To_char,To_number,To_date are three functions which perform
change of data types.
1. To_char()
This function is used to typecast a numeric value or date to character value with

a format model.
Syntax
To_char(number1,[format],[nls_paramete])
Consider the below select query.The query format the Hire_date & salary columns of

employees table using To_char function.
select first_name,To_char(hire_date,'Month DD,YYYY') hire_date,To_char

(salary,'$99999.99') salary
from employees
where rownum < 5;

select to_char(sysdate,'Month') from dual;

Elements Of the Date Model Function.







The To_number function converts a character value to a numeric data type.
if the string being converted contains non numeric characters, the function return an error.
Syntax
To_number(string1,[format],[nls_parameter])

SELECT TO_NUMBER('129.24', '9G999D99')
FROM DUAL;
TO_NUMBER('129.24','9G999D99')
------------------------------
129.24

To_date Function

This function takes character values as input & return formatted date
Equivalent of the same.
To_date function allows users to enter a date in any format, then it converts the entry
Into the default format by oracle server.

Syntax:-To_date(strin1,[format_mask],[nls_language])

SELECT TO_DATE('July 15, 1987, 09:00 A.M.', 'Month dd, YYYY, HH:MI A.M.', 'NLS_DATE_LANGUAGE = American')
FROM DUAL
/





How To Add Comments on a Table

Add a Comments on a Table in Oracle

Use the comment statement to add to te data dictinary a comment about a table or table column,view.

To insert an explanatory remark on Employees table,you might issue the following statement.

COMMENT ON TABLE employees
IS 'Employee Information Mumbai '
/

To insert an explanatory remark on salary column of the Employees table,you might issue the following statement.

comment on column employee.salary is 'Salary of an employees';
/
How to get the table comments through sql statement.



select table_name, comments
from user_tab_comments
where table_name = 'EMPLOYEES'

To drop comment , issue follwing statement.

comment on column employees.salary is '';