0% found this document useful (0 votes)
50 views13 pages

Variables and Built-In Functions: CIS 331: Introduction To Database Systems

This document discusses built-in functions and variables in SQL*Plus, including string functions like UPPER(), LOWER(), and SUBSTR(); the SYSDATE() function; defining variables; using PROMPT and ACCEPT for user input; and TTITLE, BTITLE, and BREAK commands for formatting output pages. Some examples provided demonstrate getting substrings, concatenating strings, formatting dates, defining and using variables, setting titles and footers, and breaking output across multiple pages.

Uploaded by

Nelu Badalan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views13 pages

Variables and Built-In Functions: CIS 331: Introduction To Database Systems

This document discusses built-in functions and variables in SQL*Plus, including string functions like UPPER(), LOWER(), and SUBSTR(); the SYSDATE() function; defining variables; using PROMPT and ACCEPT for user input; and TTITLE, BTITLE, and BREAK commands for formatting output pages. Some examples provided demonstrate getting substrings, concatenating strings, formatting dates, defining and using variables, setting titles and footers, and breaking output across multiple pages.

Uploaded by

Nelu Badalan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Variables and built-in functions

CIS 331: Introduction to Database Systems

Topics:
SQL*Plus string functions
UPPER() LOWER() SUBSTR()

SYSDATE() function Variables in SQL*Plus PROMPT ... ACCEPT ... TTITLE, BTITLE BREAK
Vladimir Vacic, Temple University
2

String functions
Some basic string manipulation functions are switching case and getting substrings:
SELECT first_name, middle_name, UPPER(last_name) FROM professors ; SELECT LOWER(first_name), SUBSTR(middle_name, 1, 1), UPPER(last_name) FROM professors ;

Vladimir Vacic, Temple University

String functions
|| is used to concatenate strings:
SELECT first_name || ' ' || SUBSTR(middle_name, 1, 1) || ' ' || UPPER(last_name) FROM professors ;

Vladimir Vacic, Temple University

Sysdate
SYSDATE is used to get system date and time:
SELECT sysdate FROM dual ;

Vladimir Vacic, Temple University

Sysdate
You can format SYSDATE in any style you can imagine:
SELECT to_char(sysdate, 'mm-dd-yy') FROM dual ; SELECT to_char(sysdate, 'mm/dd/yyyy') FROM dual ; SELECT to_char(sysdate, 'hh24:mi:ss') FROM dual ;

Vladimir Vacic, Temple University

SQL*Plus variables
Introducing variables in the SQL*Plus environment:
DEFINE MY_NAME = 'Gilgamesh'; DEFINE; UNDEFINE MY_NAME; DEFINE; DEFINE MY_NAME = 'Enkidu'; SELECT '&MY_NAME' FROM dual ;

Vladimir Vacic, Temple University

Prompt Accept
PROMPT ACCEPT is used to interactively get information from the user:
PROMPT 'What is your name? ' ACCEPT your_name PROMPT &your_name

Vladimir Vacic, Temple University

TTitle
TTITLE sets the header for the output page:
COLUMN middle_name FORMAT A1; TTITLE 'All students taking CIS664' SELECT first_name, middle_name, last_name FROM students WHERE ssn IN ( SELECT student FROM students_classes WHERE class = 'CIS664' ) ;

Vladimir Vacic, Temple University

TTitle, BTitle
Similarly, BTITLE sets the footer for the output page:
BTITLE LEFT 'CIS664: Data Mining' TTITLE RIGHT 'All students taking CIS664' SELECT first_name, middle_name, last_name FROM students WHERE ssn IN ( SELECT student FROM students_classes WHERE class = 'CIS664' ) ;
Vladimir Vacic, Temple University
10

SQL.PNO
SQL.PNO is the number of the current output page:
BTITLE OFF TTITLE CENTER 'All students taking CIS664' SKIP CENTER 'Page ' SQL.PNO SKIP 2 SELECT first_name, middle_name, last_name FROM students WHERE ssn IN ( SELECT student FROM students_classes WHERE class = 'CIS664' ) ;

Vladimir Vacic, Temple University

11

Break
Something more sophisticated - list all students in all classes, one class per page:
SET PAGESIZE 15; CLEAR BRAKES; COLUMN CLASS_ID NOPRINT NEW_VALUE CLASS_ID; TTITLE CENTER 'List of all students' SKIP CENTER 'taking class ' CLASS_ID BTITLE SKIP 2; BREAK ON CLASS_ID SKIP PAGE ON CLASS_ID SELECT first_name, middle_name, last_name, class CLASS_ID FROM students_classes sC, students S WHERE SC.student = S.ssn ORDER BY class;
Vladimir Vacic, Temple University
12

TTitle again
Even more sophistication - let us use custom formatted current date in the TTITLE:
SET PAGESIZE 10 COLUMN my_date NOPRINT NEW_VALUE my_date SELECT to_char(sysdate, 'mm/dd/yyyy') my_date FROM dual ; TTITLE CENTER 'Today is &my_date' skip 3 BTITLE OFF SELECT name FROM departments ;
Vladimir Vacic, Temple University
13

You might also like