Pages

Showing posts with label Oracle 12c. Show all posts
Showing posts with label Oracle 12c. Show all posts

Friday, February 7, 2014

Automatic date management in SQL

These are some DB syntax to automate date in your queries.

SYSDATE()   - Today’s date
FIRST_DAY_OF_MONTH() - First day of the current month
LAST_DAY_OF_MONTH()  - Last day of the current month
FIRST_DAY_OF_YEAR()  - First day of the current year
LAST_DAY_OF_YEAR()   - Last day of the current year

Sunday, August 25, 2013

Local Temporary tables

Local temporary tables are valid within Modules/Schema and are bound to active SQL sessions.
The syntax to create a Local Temporary table is:
DECLARE LOCAL TEMPORARY TABLE table_name ( fields);
Example
DECLARE LOCAL TEMPORARY TABLE employee_temp
( employee_id number(10) not null,
  employee_name varchar2(50) not null,
  employee_designation  varchar2(50)
);
The above command will create a temporary "employee_temp" table.

Substring in Oracle SQL

The basic syntax for Sub-String in oracle sql is
"SUBSTR(string/column/field,start_position,length)"
and this function is available in all versions from Oracle DB 8i till Oracle DB 12 C (Oracle 12c)

If start_position is 0, then the SUBSTR function treats start_position as 1 (ie: the first position in the string).
If start_position is a positive number, then the SUBSTR function starts from the beginning of the string.
If start_position is a negative number, then the SUBSTR function starts from the end of the string and counts backwards.
If length is a negative number, then the SUBSTR function will return a NULL value.

Examples:
SUBSTR('This is a test', 6, 2)  return 'is'
SUBSTR('This is a test', 6)  return 'is a test'
SUBSTR('IntelBusi', 1, 5)  return 'Intel'
SUBSTR('IntelBusi', -4, 4)  return 'Busi'
SUBSTR('IntelBusi', -4, 3)  return 'Bus'