You are on page 1of 9

EXTRACT

EXTRACT (field FROM source)

The extract function retrieves sub-fields from date/time values, such as year or hour.
source is a value expression that evaluates to type timestamp or interval. Expressions of
type date or time will be cast to timestamp and can therefore be used in most cases.

The extract function is primarily intended for computational processing. For formatting
date/time values for display, see the Section called Formatting Functions.

field is an identifier or string that selects what field to extract from the source value.

The extract function returns values of type double precision. The following are valid
values for field:

century

The year field divided by 100

SELECT EXTRACT(CENTURY FROM TIMESTAMP '2001-02-16 20:38:40');


Result: 20

Note that the result for the century field is simply the year field divided by 100,
and not the conventional definition, which considers most years in the 1900's to
be in the twentieth century..

day

The day (of the month) field (1 - 31)

SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');


Result: 16
decade

The year field divided by 10

SELECT EXTRACT(DECADE FROM TIMESTAMP '2001-02-16 20:38:40');


Result: 200
dow

The day of the week (0 - 6; Sunday is 0) (for timestamp values only)

SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40');


Result: 5

Note that dow is available for timestamp values only.


doy

The day of the year (1 - 365/366) (for timestamp values only)

SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');


Result: 47

Note that doy is available for timestamp values only.

epoch

For date and timestamp values, the number of seconds since 1970-01-01 00:00:00
(Result may be negative.); for interval values, the total number of seconds in the
interval

SELECT EXTRACT(EPOCH FROM TIMESTAMP '2001-02-16 20:38:40');


Result: 982352320

SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');


Result: 442800
hour

The hour field (0 - 23)

SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 20:38:40');


Result: 20
microseconds

The seconds field, including fractional parts, multiplied by 1,000,000.

SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');


Result: 28500000

Note that this includes full seconds.

millennium

The year field divided by 1000

SELECT EXTRACT(MILLENNIUM FROM TIMESTAMP '2001-02-16 20:38:40');


Result: 2

Note that this is not really the millennium that the date is in.

milliseconds

The seconds field, including fractional parts, multiplied by 1000.


SELECT EXTRACT(MILLISECONDS FROM TIME '17:12:28.5');
Result: 28500

Note that this includes full seconds.

minute

The minutes field (0 - 59)

SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40');


Result: 38
month

For timestamp values, the number of the month within the year (1 - 12) ; for
interval values the number of months, modulo 12 (0 - 11)

SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');


Result: 2

SELECT EXTRACT(MONTH FROM INTERVAL '2 years 3 months');


Result: 3

SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');


Result: 1
quarter

The quarter of the year (1 - 4) that the day is in.

SELECT EXTRACT(QUARTER FROM TIMESTAMP '2001-02-16 20:38:40');


Result: 1

Note that quarter is available for timestamp values only.

second

The seconds field, including fractional parts (0 - 59)

SELECT EXTRACT(SECOND FROM TIMESTAMP '2001-02-16 20:38:40');


Result: 40

SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');


Result: 28.5

Note that a value of 60 may be valid if leap seconds are implemented by the
operating system

week
From a timestamp value, calculate the number of the week of the year that the day
is in. By definition (ISO 8601), the first week of a year contains January 4 of that
year. (The ISO week starts on Monday.) In other words, the first Thursday of a
year is in week 1 of that year.

SELECT EXTRACT(WEEK FROM TIMESTAMP '2001-02-16 20:38:40');


Result: 7
year

The year field

SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');


Result: 2001

date_part
The date_part function is the traditional PostgreSQL equivalent to the SQL-function
extract:

date_part('field', source)

Note that here the field value needs to be a string. The valid field values for date_part
are the same as for extract.

SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');


Result: 16

SELECT date_part('hour', INTERVAL '4 hours 3 minutes')


Result: 4

date_trunc
The function date_trunc is conceptually similar to the trunc function for numbers.

date_trunc('field', source)

source is a value expression of type timestamp (values of type date and time are cast
automatically). field selects to which precision to truncate the time stamp value. The
return value is of type timestamp with all fields that are less than the selected one set to
zero (or one, for day and month).

Valid values for field are:

microseconds
milliseconds
second
minute
hour
day
month
year
decade
century
millennium
SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-02-16 20:00:00+00

SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');


Result: 2001-01-01 00:00:00+00

Current Date/Time
The following functions are available to obtain the current date and/or time:

CURRENT_TIME
CURRENT_DATE
CURRENT_TIMESTAMP

Note that because of the requirements of the SQL standard, these functions must not be
called with trailing parentheses.

SELECT CURRENT_TIME;
19:07:32

SELECT CURRENT_DATE;
2001-02-17

SELECT CURRENT_TIMESTAMP;
2001-02-17 19:07:32-05

The function now() is the traditional PostgreSQL equivalent to CURRENT_TIMESTAMP.

There is also timeofday(), which returns current time to higher precision than the
CURRENT_TIMESTAMP family does:

SELECT timeofday();
Sat Feb 17 19:07:32.000126 2001 EST

timeofday() uses the operating system call gettimeofday(2), which may have
resolution as good as microseconds (depending on your platform); the other functions
rely on time(2) which is restricted to one-second resolution. For historical reasons,
timeofday() returns its result as a text string rather than a timestamp value.
It is quite important to realize that CURRENT_TIMESTAMP and related functions all return
the time as of the start of the current transaction; their values do not increment while a
transaction is running. But timeofday() returns the actual current time.

All the date/time datatypes also accept the special literal value now to specify the current
date and time. Thus, the following three all return the same result:

SELECT CURRENT_TIMESTAMP;
SELECT now();
SELECT TIMESTAMP 'now';

DCL commands are used to enforce database security in a multiple user database
environment. Two types of DCL commands are GRANT and REVOTE. Only Database
Administrator's or owner's of the database object can provide/remove privileges on a
databse object.

SQL GRANT Command


SQL GRANT is a command used to provide access or privileges on the database objects
to the users.

The Syntax for the GRANT command is:

GRANT privilege_name
ON object_name
TO {user_name |PUBLIC |role_name}
[WITH GRANT OPTION];

 privilege_name is the access right or privilege granted to the user. Some of the
access rights are ALL, EXECUTE, and SELECT.
 object_name is the name of an database object like TABLE, VIEW, STORED
PROC and SEQUENCE.
 user_name is the name of the user to whom an access right is being granted.
 user_name is the name of the user to whom an access right is being granted.
 PUBLIC is used to grant access rights to all users.
 ROLES are a set of privileges grouped together.
 WITH GRANT OPTION - allows a user to grant access rights to other users.

For Eample: GRANT SELECT ON employee TO user1;This command grants a


SELECT permission on employee table to user1.You should use the WITH GRANT
option carefully because for example if you GRANT SELECT privilege on employee
table to user1 using the WITH GRANT option, then user1 can GRANT SELECT
privilege on employee table to another user, such as user2 etc. Later, if you REVOKE the
SELECT privilege on employee from user1, still user2 will have SELECT privilege on
employee table.

SQL REVOKE Command:


The REVOKE command removes user access rights or privileges to the database objects.

The Syntax for the REVOKE command is:

REVOKE privilege_name
ON object_name
FROM {user_name |PUBLIC |role_name}

For Eample: REVOKE SELECT ON employee FROM user1;This commmand will


REVOKE a SELECT privilege on employee table from user1.When you REVOKE
SELECT privilege on a table from a user, the user will not be able to SELECT data from
that table anymore. However, if the user has received SELECT privileges on that table
from more than one users, he/she can SELECT from that table until everyone who
granted the permission revokes it. You cannot REVOKE privileges if they were not
initially granted by you.

Privileges and Roles:


Privileges: Privileges defines the access rights provided to a user on a database object.
There are two types of privileges.

1) System privileges - This allows the user to CREATE, ALTER, or DROP database
objects.
2) Object privileges - This allows the user to EXECUTE, SELECT, INSERT, UPDATE,
or DELETE data from database objects to which the privileges apply.

Few CREATE system privileges are listed below:

System
Description
Privileges
allows users to create the specified
CREATE object
object in their own schema.
CREATE ANY allows users to create the specified
object object in any schema.

The above rules also apply for ALTER and DROP system privileges.

Few of the object privileges are listed below:

Object Description
Privileges
INSERT allows users to insert rows into a table.
allows users to select data from a
SELECT
database object.
UPDATE allows user to update data in a table.
allows user to execute a stored
EXECUTE
procedure or a function.

Roles: Roles are a collection of privileges or access rights. When there are many users in
a database it becomes difficult to grant or revoke privileges to users. Therefore, if you
define roles, you can grant or revoke privileges to users, thereby automatically granting
or revoking privileges. You can either create Roles or use the system roles pre-defined by
oracle.

Some of the privileges granted to the system roles are as given below:

System Role Privileges Granted to the Role


CREATE TABLE, CREATE VIEW,
CONNECT CREATE SYNONYM, CREATE
SEQUENCE, CREATE SESSION etc.
CREATE PROCEDURE, CREATE
SEQUENCE, CREATE TABLE,
RESOURCE CREATE TRIGGER etc. The primary
usage of the RESOURCE role is to
restrict access to database objects.
DBA ALL SYSTEM PRIVILEGES

Creating Roles:
The Syntax to create a role is:

CREATE ROLE role_name


[IDENTIFIED BY password];

For example: To create a role called "developer" with password as "pwd",the code will
be as follows

CREATE ROLE testing


[IDENTIFIED BY pwd];

It's easier to GRANT or REVOKE privileges to the users through a role rather than
assigning a privilege direclty to every user. If a role is identified by a password, then,
when you GRANT or REVOKE privileges to the role, you definetely have to identify it
with the password.

We can GRANT or REVOKE privilege to a role as below.


For example: To grant CREATE TABLE privilege to a user by creating a testing role:

First, create a testing Role

CREATE ROLE testing

Second, grant a CREATE TABLE privilege to the ROLE testing. You can add more
privileges to the ROLE.

GRANT CREATE TABLE TO testing;

Third, grant the role to a user.

GRANT testing TO user1;

To revoke a CREATE TABLE privilege from testing ROLE, you can write:

REVOKE CREATE TABLE FROM testing;

The Syntax to drop a role from the database is as below:

DROP ROLE role_name;

For example: To drop a role called developer, you can write:

DROP ROLE testing;

You might also like