Tuesday, 26 November 2013

Changing NLS_Language NLS_Territory NLS_Calendar

Bismillahir Rahmanir Raheem

When I installed my Oracle database I probably selected US or I think default installation is US.
But you can change it as you like later
We will  see how to change it

You can see all your NLS_parameters from a dynamic view called NLS_Parameters

select *
from NLS_Parameters;

You will see in the results parameters like
NLS_Language    AMERICAN
NLS_Calendar    GREGORIAN
NLS_Territory     AMERICA

and others

so lets change the default values

alter session set NLS_Language='Arabic';

alter session set NLS_Territory='United Arab Emirates';

alter session set NLS_Calendar='Arabic Hijrah';


 Similarly you can change any NLS_parameters as you like

You might  want to know what are the values that you can assign to these NLS parameters

here is link to oracle documentation to help you

Oracle Database Globalization Support

Working with oracle timezone format TZR TZH TZM

Bismillahir Rahmanir Raheem

Today we will look to some format that are used to convert or outputting more precise information.

TZH--
This gives the hour from the timezone and is only valid with timestamp and interval formats

TZM----
This gives the minute from the timezone and is only valid with timestamp and interval formats

TZR---
This gives the time zone region information and is only valid with timestamp and interval formats

alter session set time_zone='asia/calcutta';

select to_char(systimestamp,'TZR-TZH-TZM')
from dual;

this will give us

Asia/calcutta-+05-30

NOte-- remember all these formats are only valid with timestamp and interval types not with date type

Monday, 25 November 2013

INSERTING ROWs INTO TABLE FROM SELECTING ROWS FROM ANOTHER TABLE

Bismillahir Rahmanir Raheem

We will insert rows into a table from retreiving rows from another table

Create table table1(id number,name varchar2(20));

insert into table1 values(111,'Mahtab Alam');
insert into table1 values(222,'Aftab Alam');
insert  into table1 values(333,'Sdre Alam');

Now we have three rows in table1 .
Lets add these rows into table2

create table table2(RollNo number,Student_name varchar2(20));

insert into table2
select id,name
from table1;

or

create table table3(pid number);

insert into table3(pid)
select id
from table1;

Oracle To_date() function for converting into date datatype

Bismillahir Rahmanir Raheem

Suppose I have table Entry as

Create table Entry(Entered number);

insert into Entry values(12122012);
insert into Entry values(10102013);
insert into Entry values(11112013);

select * from Entry;





Now lets change it to some meaningful  way , lets change it to date which it actually represents

SELECT  ENTERED,TO_DATE(ENTERED,'DDMMYYYY')
FROM     ENTRY;

HOW TO FIND OUT THE AVAILABLE TIMEZONES IN ORACLE SQL

BISMILLAHIR RAHMANIR RAHEEM

You can query the dynamic view named  as   v$timezone_names to get the list  of
available timezones .

select *
from v$timezone_names;

This will give you a huge list  of timezone names .
Now you can use these names to change your timezone

You can change your timezone either by giving timezone offset or timezone regional name.

Its better to use timezone regional names

Here is the query

alter session set time_zone='ASIA/CALCUTTA';


Sunday, 24 November 2013

Using where clause with group by

Bismillahir Rahmanir Raheem

Consider the table student where  I have three columns
student
dept
gender



select dept,count(student)
from   student
where gender='M'
group by dept
having count(student) >= 2;




In the above query I have used where before group by what it does is ,it ignores those rows from the
consideration of group by clause which does not satisfy the where condition.

So group by only makes group that satisfy the condition , this gives you a way to filter your group before they are made and by using having clause with group by we can filter goups after they have been made.

Saturday, 23 November 2013

Correlated SubQuery -The best way irrespective of database you are working with

Bismillahir Rahmanir Raheem

Now I am going to talk about the best way to deal with Queries Like finding Second max or min or any other Salary position and the best thing about this approach is that it is irrespective of database.
So it will work with MySQL,Oracle,MS SQL and any other that you probably know.

this is our table account

Name                           Salary
A                                  1000
B                                   500
C                                   800
D                                  1500
E                                  2000
F                                  3000

Before this we were using independent subqueries but in this approach we will use Correlated SubQuery.

So what is the difference between independent subquery and correlated subquery.
Independent subquery can execute even if the outer query is not present
But Correlated subquery will only execute when there is an outer query present .
So Correlated Subquery is only meaningful when there is an outer query.


So How can we find Maximum salary using Correlated subquery

How many salary is higher than Maximum salary ?
Answer : 0

Here is the solution query

Select salary
from account x
where 0=( select count(*)
                 from account y
                 where y.salary > x.salary);

So what about finding Minimum salary
How many salary is higher than Minimum Salary ?
Answer : count(*)-1

So first find number of rows inn the database using

Select count(*) from account;

So lets calculate second Highest salary
How many salary is higher than Second Highest Salary ?
Answer : 1

Select salary
from account x
where 1=( select count(*)
                 from account y
                 where y.salary > x.salary);