× PL/SQL - Overview PL/SQL - Basic Syntax PL/SQL - Data Types PL/SQL - Variables PL/SQL - Constants and Literals PL/SQL - Operators PL/SQL - Conditions PL/SQL -ifelse PL/SQL -elsif PL/SQL -nestedif PL/SQL -Case PL/SQL -Searched Case PL/SQL -Basic Loop PL/SQL -For Loop PL/SQL -While Loop PL/SQL - Strings PL/SQL - Arrays PL/SQL - Procedures PL/SQL - Functions PL/SQL - Cursors PL/SQL - Records PL/SQL - Exceptions PL/SQL - Triggers PL/SQL - Packages PL/SQL - Collections PL/SQL - Transactions PL/SQL - Date & Time PL/SQL - DBMS Output PL/SQL - Object Oriented
  • iconPLSQL Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

PL/SQL - Strings



PL/SQL - Strings
The string in PL/SQL 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. PL/SQL offers three kinds of strings

1.Fixed-length strings
In such strings, programmers specify the length while declaring the string. The string is right-padded with spaces to the length so specified.
2.Variable-length strings
In such strings, a maximum length up to 32,767, for the string is specified and no padding takes place.
3.Character large objects (CLOBs)
These are variable-length strings that can be up to 128 terabytes.

PL/SQL 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, VARCHAR2, NVARCHAR2, CLOB, and NCLOB. The datatypes prefixed with an 'N' are 'national character set' datatypes, that store Unicode character data.
If you need to declare a variable-length string, you must provide the maximum length of that string. For example, the VARCHAR2 data type. The following example illustrates declaring and using some string variables
DECLARE 
   name varchar2(20); 
   company varchar2(30); 
   introduction clob; 
   choice char(1); 
BEGIN 
   name := 'Sateesh'; 
   company := 'MSK Technologies'; 
   introduction := ' Hello! I''m Sateesh from MSK Technologies.'; 
   choice := 'y'; 
   IF choice = 'y' THEN 
      dbms_output.put_line(name); 
      dbms_output.put_line(company); 
      dbms_output.put_line(introduction); 
   END IF; 
END; 
/
Output:
Sateesh
Msk Technologies
Hello! I''m Sateesh from MSK Technologies.

To declare a fixed-length string, use the CHAR datatype. Here you do not have to specify a maximum length for a fixed-length variable. If you leave off the length constraint, Oracle Database automatically uses a maximum length required. The following two declarations are identical
red_flag CHAR(1) := 'Y'; 
 red_flag CHAR   := 'Y';
 

PL/SQL String Functions and Operators
PL/SQL offers the concatenation operator (||) for joining two strings. The following table provides the string functions provided by PL/SQL

ASCII(x);
Returns the ASCII value of the character x.
CHR(x);
Returns the character with the ASCII value of x.
CONCAT(x, y);
Concatenates the strings x and y and returns the appended string.
INITCAP(x);
Converts the initial letter of each word in x to uppercase and returns that string.
INSTR(x, find_string [, start] [, occurrence]);
Searches for find_string in x and returns the position at which it occurs.
INSTRB(x);
Returns the location of a string within another string, but returns the value in bytes.
LENGTH(x);
Returns the number of characters in x.
LENGTHB(x);
Returns the length of a character string in bytes for single byte character set.
LOWER(x);
Converts the letters in x to lowercase and returns that string.
LPAD(x, width [, pad_string]) ;
Pads x with spaces to the left, to bring the total length of the string up to width characters.
LTRIM(x [, trim_string]);
Trims characters from the left of x.
NANVL(x, value);
Returns value if x matches the NaN special value (not a number), otherwise x is returned.
NLS_INITCAP(x);
Same as the INITCAP function except that it can use a different sort method as specified by NLSSORT.
NLS_LOWER(x) ;
Same as the LOWER function except that it can use a different sort method as specified by NLSSORT.
NLS_UPPER(x);
Same as the UPPER function except that it can use a different sort method as specified by NLSSORT.
NLSSORT(x);
Changes the method of sorting the characters. Must be specified before any NLS function; otherwise, the default sort will be used.
NVL(x, value);
Returns value if x is null; otherwise, x is returned.
NVL2(x, value1, value2);
Returns value1 if x is not null; if x is null, value2 is returned.
REPLACE(x, search_string, replace_string);
Searches x for search_string and replaces it with replace_string.
RPAD(x, width [, pad_string]);
Pads x to the right.
RTRIM(x [, trim_string]);
Trims x from the right.
SOUNDEX(x) ;
Returns a string containing the phonetic representation of x.
SUBSTR(x, start [, length]);
Returns a substring of x that begins at the position specified by start. An optional length for the substring may be supplied.
SUBSTRB(x);
Same as SUBSTR except that the parameters are expressed in bytes instead of characters for the single-byte character systems.
TRIM([trim_char FROM) x);
Trims characters from the left and right of x.
UPPER(x);
Converts the letters in x to uppercase and returns that string.

Example Program
DECLARE 
   greetings varchar2(11) := 'hello world'; 
BEGIN 
   dbms_output.put_line(UPPER(greetings)); 
    
   dbms_output.put_line(LOWER(greetings)); 
    
   dbms_output.put_line(INITCAP(greetings)); 
    
   /* retrieve the first character in the string */ 
   dbms_output.put_line ( SUBSTR (greetings, 1, 1)); 
    
   /* retrieve the last character in the string */ 
   dbms_output.put_line ( SUBSTR (greetings, -1, 1)); 
    
   /* retrieve five characters,  
      starting from the seventh position. */ 
   dbms_output.put_line ( SUBSTR (greetings, 7, 5)); 
    
   /* retrieve the remainder of the string, 
      starting from the second position. */ 
   dbms_output.put_line ( SUBSTR (greetings, 2)); 
     
   /* find the location of the first "e" */ 
   dbms_output.put_line ( INSTR (greetings, 'e')); 
END; 
/
Output:
HELLO WORLD
hello world
Hello World
h
d
World
ello World
2
Example Program
DECLARE 
   greetings varchar2(30) := '    Hello World    '; 
BEGIN 
   dbms_output.put_line(RTRIM(greetings,'.')); 
   dbms_output.put_line(LTRIM(greetings, '.')); 
   dbms_output.put_line(TRIM( '.' from greetings)); 
END; 
/
Output:

......Hello World
Hello World.....
Hello World

Key Points

  • PL/SQL - Strings
  • Image