



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A tutorial on formatting output and manipulating strings in oracle. It covers various sql*plus commands for editing queries, datatypes, and string functions. Students will learn how to use concatenation, column aliases, speech marks and quotes, and various string functions like initcap, instr, length, and upper.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Page 1 of 6
This tutorial will cover the topics recently covered in Lectures. Joining Outputs with Concatenation, Simple Arithmetic, Column Aliases, Speech Marks andQuotes, String functions.
SQLPlus Commands* (as promised). Use the following commands to edit queries that exist within the SQL Buffer. L Last, this tells Oracle to call up the last SQLcommand. I Insert. This command can be used to add linesto a SQL command that you have called up using L. A Append, used to append to the end of a line ina query D Delete, used to delete current line C/???/!!!! Change, the command shown will replace thefirst occurrence of ??? with the string !!! in the last SQL command used. Say you type a query… SELECT table_names FROM all_table; When you attempt to execute this, Oracle will tell you it is not aware of a table by this name.Forgot the S on all_tables. An asterix will be placed under where the error occurred.
To edit the query, first print it to the screen using L. Once listed, an asterix will be placedbeside the last line, indicating that you may edit that line. To append an S to the name of the table, type… A s The space is very important. Now run the query using the forward slash /. Another error on the first line. Use L again. The asterix still appears beside the last line of the query, No. 2. Wewant to edit line 1, and get rid of the unwanted S at the end of the column name. Type 1 and hit return, note that is the line you can edit. To drop the S, or change it so that it is replaced by an empty space, type C/names/name SQL*Plus will go through line 1, until it finds the string ‘names’, and then, replace it with‘name’. Note that if we opted to use…
C/s/ As in replace ‘s’ with nothing, SQL*Plus would have carried out this operation on the first ‘s’ itcame across, in the SELECT.
Use L to list the query again, and run it with /.
Page 2 of 6
Datatypes. We already noticed how some columns within tables within Oracle have different definitions to others. This could be seen when we described a table. DESC all_tables I will give a brief description here now, so that you will understand the differences that apply when trying to manipulate the data within columns. VARCHAR2(w) Variable length character values up to width w.Maximum width is 2000 characters. * CHAR(w) Fixed length character values of width w.Default width is 1. Max: 255. * NUMBER Floating point number with a precision of 38significant digits.* NUMBER(w) Integer numbers of precision w. NUMBER(w,s) Numbers with a precision w and scales s.Precision represents the maximum significant digits allowed, which may not exceed 38. Scaleis the number of decimal places recorded to the right of the point *. DATE Date values from January 1, 4712 BC toDecember 31,4712 AD. Time information is also stored. * LONG Variable length character values up to 2 Gigs.Only one long column allowed per table. * RAW and LONG RAW Equivalent to VARCHAR2 (but, maximum widthis 255) and LONG respectively, but used for storing byte-oriented or binary data such asgraphics images or digital sound. *
SELECT column1 || column FROM table ; Here is an example using the all_tables table we used last week. SELECTFROM table_name||ownerall_tables;
You can also include text within your output through the use of ‘||’ and the single quote. SELECTFROM table_name||’ is a table name’all_tables;
Note that a space is included after the first quote. This means a clearer output. Anotherexample that leads to a more meaningful output is:
SELECTFROM table_name||’ is owned by ‘||ownerall_tables;
Page 4 of 6
You can also apply more than one function to different columns within the same query. SELECTFROM RPAD(table_name, 30, ‘.’), INITCAP(owner)all_tables;
Try using LPAD in a similar example. LTRIM and RTRIM can be used in a similar fashion. Take this example; it might be a goodidea to retrieve all owner information from all_tables first, for comparison purposes…
SELECTFROM LTRIM(owner, ‘S’)all_tables;
Or… SELECTFROM RTRIM(owner, ‘T’)all_tables;
Or to make things clearer… SELECTFROM owner, RTRIM(owner, ‘T’)all_tables;
To use SUBSTR to cut out a piece of data from a column, the following notation is applied… SELECTFROM SUBSTR( table ; column , start [ , count ])
Specify the column you want to cut a string from, the character position you want to start the extract from, and if required, how many letters you want to clip out. Example… SELECTFROM owner, SUBSTR(owner, 3)all_tables;
Or… SELECTFROM owner, SUBSTR(owner, 3, 2)all_tables;
You can also combine functions and apply several at a time to one column, using the outputfrom one function as the input for another…
SELECTFROM owner, LENGTH(SUBSTR(owner, 3))all_tables;
INSTR is used to find the occurrence of a character or a set of characters within a string. The notation is…
Page 5 of 6