example
select INITCAP('sample initcap') from dual
result: Sample Initcap
By using '[[:alpha:]]' with regular expressions it is possible to findalphabetic characters. By adding {3} at the end we limit the length of matchingalphabetic characters.
Select * from test_table where REGEXP_LIKE(test_column, '[[:alpha:]]');Select * from test_table where REGEXP_LIKE(test_column, '[[:alpha:]]{3}');
By using '[[:alnum:]]' with regular expressions it is possible to find
alphanumeric characters.By adding {3} at the end we limit the length of
matching alphanumeric characters.
Select * from test_table where REGEXP_LIKE(test_column, '[[:alnum:]]');
Select * from test_table where REGEXP_LIKE(test_column, '[[:alnum:]]{3}');
select regexp_replace('Tesstt', '(.)\1+','\1')
from dual;
Result: Test
CREATE [OR REPLACE] PROCEDURE procedure_name [ (parameter [,parameter]) ] IS [declaration_section] BEGIN executable_section [EXCEPTION exception_section] END [procedure_name];
CREATE OR REPLACE Procedure UpdateCourse ( name_in IN varchar2 ) IS cnumber number; cursor c1 is SELECT course_number FROM courses_tbl WHERE course_name = name_in; BEGIN open c1; fetch c1 into cnumber; if c1%notfound then cnumber := 9999; end if; INSERT INTO student_courses ( course_name, course_number ) VALUES ( name_in, cnumber ); commit; close c1; EXCEPTION WHEN OTHERS THEN raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM); END;
DROP PROCEDURE procedure_name;
DROP PROCEDURE UpdateCourse;
CREATE [OR REPLACE] FUNCTION function_name [ (parameter [,parameter]) ] RETURN return_datatype IS | AS [declaration_section] BEGIN executable_section [EXCEPTION exception_section] END [function_name];
CREATE OR REPLACE Function FindCourse ( name_in IN varchar2 ) RETURN number IS cnumber number; cursor c1 is SELECT course_number FROM courses_tbl WHERE course_name = name_in; BEGIN open c1; fetch c1 into cnumber; if c1%notfound then cnumber := 9999; end if; close c1; RETURN cnumber; EXCEPTION WHEN OTHERS THEN raise_application_error(-20001,'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM); END;
SELECT course_name, FindCourse(course_name) AS course_id FROM courses WHERE subject = 'Mathematics';
DROP FUNCTION function_name;
DROP FUNCTION FindCourse;
--
symbol--
symbol is:-- comment goes here
--
symbol must be at the end of a line in your SQL statement with a line break after it. This method of commenting can only span a single line within your SQL and must be at the end of the line./*
and */
symbols/*
and */
symbols is:/* comment goes here */
/*
symbol and ends with */
and can be anywhere in your SQL statement. This method of commenting can span several lines within your SQL.SELECT websites.site_name /* Author: TechOnTheNet.com */ FROM websites;
SELECT /* Author: TechOnTheNet.com */ websites.site_name FROM websites;
SELECT websites.site_name /* Author: TechOnTheNet.com */ FROM websites;
SELECT websites.site_name -- Author: TechOnTheNet.com FROM websites;
SELECT websites.site_name /* * Author: TechOnTheNet.com * Purpose: To show a comment that spans multiple lines in your SQL statement. */ FROM websites;
SELECT websites.site_name /* Author: TechOnTheNet.com Purpose: To show a comment that spans multiple lines in your SQL statement. */ FROM websites;
/*
symbol is a comment until it reaches the */
symbol, even if it spans multiple lines within the SQL statement. So in this example, the comment will span across 2 lines.CREATE VIEW view_name AS SELECT columns FROM tables [WHERE conditions];
CREATE VIEW sup_orders AS SELECT suppliers.supplier_id, orders.quantity, orders.price FROM suppliers INNER JOIN orders ON suppliers.supplier_id = orders.supplier_id WHERE suppliers.supplier_name = 'IBM';
SELECT * FROM sup_orders;
CREATE OR REPLACE VIEW view_name AS SELECT columns FROM table [WHERE conditions];
CREATE or REPLACE VIEW sup_orders AS SELECT suppliers.supplier_id, orders.quantity, orders.price FROM suppliers INNER JOIN orders ON suppliers.supplier_id = orders.supplier_id WHERE suppliers.supplier_name = 'Microsoft';
DROP VIEW view_name;
DROP VIEW sup_orders;
DECLARE LOCAL TEMPORARY TABLE table_name ( column1 datatype [ NULL | NOT NULL ], column2 datatype [ NULL | NOT NULL ], ... );
DECLARE LOCAL TEMPORARY TABLE suppliers_temp ( supplier_id int NOT NULL, supplier_name char(50) NOT NULL, contact_name char(50) );
CREATE GLOBAL TEMPORARY TABLE table_name ( column1 datatype [ NULL | NOT NULL ], column2 datatype [ NULL | NOT NULL ], ... );
CREATE GLOBAL TEMPORARY TABLE suppliers_temp ( supplier_id numeric(10) NOT NULL, supplier_name char(50) NOT NULL, contact_name char(50) );
DROP TABLE table_name;
DROP TABLE suppliers;
DROP TABLE totn.contacts;
ALTER TABLE table_name ADD column_name column-definition;
ALTER TABLE supplier ADD supplier_name char(50);
ALTER TABLE table_name ADD (column_1 column-definition, column_2 column-definition, ... column_n column_definition);
ALTER TABLE supplier ADD (supplier_name char(50), city char(45));
ALTER TABLE table_name MODIFY column_name column_type;
ALTER TABLE table_name ALTER COLUMN column_name column_type;
ALTER TABLE table_name ALTER COLUMN column_name TYPE column_definition;
ALTER TABLE supplier MODIFY supplier_name char(100) NOT NULL;
ALTER TABLE supplier MODIFY supplier_name VARCHAR(100) NOT NULL;
ALTER TABLE supplier ALTER COLUMN supplier_name VARCHAR(100) NOT NULL;
ALTER TABLE supplier ALTER COLUMN supplier_name TYPE CHAR(100), ALTER COLUMN supplier_name SET NOT NULL;
ALTER TABLE table_name MODIFY (column_1 column_type, column_2 column_type, ... column_n column_type);
ALTER TABLE table_name MODIFY column_1 column_definition [ FIRST | AFTER column_name ], MODIFY column_2 column_definition [ FIRST | AFTER column_name ], ... ;
ALTER TABLE table_name ALTER COLUMN column_name TYPE column_definition, ALTER COLUMN column_name TYPE column_definition, ... ;
ALTER TABLE supplier MODIFY (supplier_name char(100) NOT NULL, city char(75));
ALTER TABLE supplier MODIFY supplier_name VARCHAR(100) NOT NULL, MODIFY city VARCHAR(75);
ALTER TABLE supplier ALTER COLUMN supplier_name TYPE CHAR(100), ALTER COLUMN supplier_name SET NOT NULL, ALTER COLUMN city TYPE CHAR(75);
ALTER TABLE table_name DROP COLUMN column_name;
ALTER TABLE supplier DROP COLUMN supplier_name;
ALTER TABLE table_name RENAME COLUMN old_name TO new_name;
sp_rename 'table_name.old_column', 'new_name', 'COLUMN';
ALTER TABLE table_name CHANGE COLUMN old_name TO new_name;
ALTER TABLE supplier RENAME COLUMN supplier_name TO sname;
sp_rename 'supplier.supplier_name', 'sname', 'COLUMN';
ALTER TABLE supplier CHANGE COLUMN supplier_name sname VARCHAR(100);
ALTER TABLE table_name RENAME TO new_table_name;
sp_rename 'table_name', 'new_table_name';
ALTER TABLE supplier RENAME TO vendor;
sp_rename 'supplier', 'vendor';