Question: LAB 4 Content PL / SQL - Strings The string in PL / SQL is actually a sequence of characters with an optional size specification.
LAB Content
PLSQL Strings
The string in PLSQL is actually a sequence of characters with an optional size specification. The characters could be numeric, letters, blank, special characters or a combination of all. PLSQL offers three kinds of strings
Fixedlength strings In such strings, programmers specify the length while declaring the string. The string is rightpadded with spaces to the length so specified.
Variablelength strings In such strings, a maximum length up to for the string is specified and no padding takes place.
Character large objects CLOBs These are variablelength strings that can be up to terabytes.
PLSQL strings could be either variables or literals. A string literal is enclosed within quotation marks. For example,
'This is a string literal. Or 'hello world'
To include a single quote inside a string literal, you need to type two single quotes next to one another. For example,
'this isn't what it looks like'
Declaring String Variables
Oracle database provides numerous string datatypes, such as CHAR, NCHAR, VARCHAR NVARCHAR CLOB, and NCLOB. The datatypes prefixed with anNarenational character set'datatypes that store Unicode character data.
If you need to declare a variablelength string, you must provide the maximum length of that string. For example, the VARCHAR data type. The following example illustrates declaring and using some string variables
DECLARE
name varchar;
company varchar;
introduction clob;
choice char;
BEGIN
name : 'John Smith';
company : 'Infotech';
introduction : Hello! Im John Smith from Infotech.;
choice :y;
IF choice y THEN
dbmsoutput.putlinename;
dbmsoutput.putlinecompany;
dbmsoutput.putlineintroduction;
END IF;
END;
When the above code is executed at the SQL prompt, it produces the following result
John Smith
Infotech
Hello! Im John Smith from Infotech.
PLSQL procedure successfully completed
To declare a fixedlength string, use the CHAR datatype. Here you do not have to specify a maximum length for a fixedlength variable. If you leave off the length constraint, Oracle Database automatically uses a maximum length required. The following two declarations are identical
redflag CHAR :Y;
redflag CHAR :Y;
PLSQL String Functions and Operators
PLSQL offers the concatenation operatorfor joining two strings. The following table provides the string functions provided by PLSQL
SNo Function & Purpose
ASCIIx;
Returns the ASCII value of the character x
CHRx;
Returns the character with the ASCII value of x
CONCATx y;
Concatenates the strings x and y and returns the appended string.
INITCAPx;
Converts the initial letter of each word in x to uppercase and returns that string.
INSTRx findstring start occurrence;
Searches forfindstringin x and returns the position at which it occurs.
INSTRBx;
Returns the location of a string within another string, but returns the value in bytes.
LENGTHx;
Returns the number of characters in x
LENGTHBx;
Returns the length of a character string in bytes for single byte character set.
LOWERx;
Converts the letters in x to lowercase and returns that string.
LPADx width padstring ;
Padsxwith spaces to the left, to bring the total length of the string up to width characters.
LTRIMx trimstring;
Trims characters from the left ofx
NANVLx value;
Returns value if x matches the NaN special value not a number otherwisexis returned.
NLSINITCAPx;
Same as the INITCAP function except that it can use a different sort method as specified by NLSSORT.
NLSLOWERx ;
Same as the LOWER function except that it can use a different sort method as specified by NLSSORT.
NLSUPPERx;
Same as the UPPER function except that it can use a different sort method as specified by NLSSORT.
NLSSORTx;
Changes the method of sorting the characters. Must be specified before any NLS function; otherwise, the default sort will be used.
NVLx value;
Returns value ifxis null; otherwise, x is returned.
NVLx value value;
Returns value if x is not null; if x is null, value is returned.
REPLACEx searchstring, replacestring;
Searchesxfor searchstring and replaces it with replacestring.
RPADx width padstring;
Padsxto the right.
RTRIMx trimstring;
Trimsxfrom the right.
SOUNDEXx ;
Returns a string containing the phonetic representation ofx
SUBSTRx start length;
Returns a substring ofxthat begins at the position specified by start. An optional length for the substring may be supplied.
SUBSTRBx;
Same as SUBSTR except that the parameters are expressed in bytes instead of characters for the singlebyte character systems.
TRIMtrimchar FRO
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
