PLSQL

PL/SQL Strings

PL/SQL Strings

Strings are the sequence of characters with an optional size specification. These characters could be numeric, letters, blank, special characters or a combination of all PL/SQL offers three different kinds of strings:

1.Fixed-length strings

In strings, programmers should specify the length while declaring the strings, the string will be right padded with spaces to the length that is specified.

2.Variable-length strings

In these types of strings, we have maximum length up to 32,767 for the strings that are specified and therefore, no padding takes place.

3.Character large objects (CLOBs)

These types of strings are also variable that can take up to 128 terabytes. These strings can either be variables or literals, and these are need to be enclosed in single quotes next to each other.  

Declaring String Variables

We have many String datatypes such as CHAR, NCHAR, VARCHAR2, NVARCHAR2, CLOB, and NCLOB provided by oracle database. And also, these datatypes are fixed with an ‘N’ are the ‘national character set’ datatypes. Incase, we need to declare a variable-length string, we should provide the maximum length of that string. And to declare to fixed-length string, we use the CHAR datatype. We need not specify a maximum length for a fixed-length variable.

Example:
DECLARE 
   name varchar2(20); 
   company varchar2(30); 
   introduction clob; 
   choice char(1); 
BEGIN 
   name := 'sai'; 
   company := 'TCS'; 
   introduction := ' Hello! I''m sai from TCS.'; 
   choice := 'a'; 
   IF choice = 'a' THEN 
      dbms_output.put_line(name); 
      dbms_output.put_line(company); 
      dbms_output.put_line(introduction); 
   END IF; 
END; 
/
Output:
sai 
TCS
Hello! I'm sai from TCS.  

PL/SQL procedure successfully completed

PL/SQL String Functions and Operators

We have different types of string functions provided by PL/SQL. We also have a concatenation operator (||). 

  • ASCII(x); - It returns the ASCII value of character x. 
  • CHR(x); - It returns the character and also the value of x.
  • CONCAT(x, y); - it concatenates the strings x and y and returns the appended string.
  • INITCAP(x); - this function converts the initial letter of each word in x to its uppercase and then returns the string.
  • INSTR(x, find_string [, start] [, occurrence]); - this function searches the find_string in x and also returns their position at which it occurs.
  • INSTRB(x); - this returns the location of the string within another string and also returns the values in bytes.
  • LENGTH(x); - it returns the number of characters in x.
  • LENGTHB(x); - it returns the length of a character string in bytes for single byte character set. 
  • LOWER(x); - it converts the letters in x to its lowercase and then returns that string.  
  • LPAD(x, width [, pad_string]); - this function pads x with spaces to that of the left, and then bring the total length of the string up to width of the characters.
  • LTRIM(x [, trim_string]); - this trims the characters that are left to the x.
  • NANVL(x, value); - this function returns the value if x matches the NAN special value (not a number), otherwise the x is returned.
  • NLS_INITCAP(x); - this function is same as the INITCAP function except that this function uses a different sort method as specified by NLSSORT. 
  • NLS_LOWER(x); - it is also same as LOWER function except that this function uses a different sort method as specified by NLSSORT. 
  • NLS_UPPER(x); - it is also same as UPPER function except that this function uses a different sort method as specified by NLSSORT.
  • NLS_SORT(x); - this function changes the method of sorting the characters, and this should be specified before any NLS function, else the default sort method will be used.
  • NVL(x, value); - this function returns if the value is null, else x is returned.
  • NVL2(x, value1, value2); - this function returns value1 when x is not null, otherwise value2 is returned.
  • REPLACE(x, search_string, replace_string); - this function searches x for search_string and then replaces with the replace_string.
  • RPAD(x, width [, pad_string]); - this function pads x to the right.
  • RTRIM(x, [, trim_string]); - this trims x from the right side.
  • SOUNDEX(x); - this function returns a string containing the phonetic representation of x.
  • SUBSTR(x, start [, length]); - this returns a substring of x that begins at the specifies position by start. There is also an optional length of the string supplied.
  • SUBSTRB(x); - this function is same as SUBSTR except for the parameters that are expressed in bytes instead of characters.
  • TRIM([trim_char FROM] x); - this trims characters from the left and also right of x.
  • UPPER(x); - this converts the letters to the uppercase and returns the string.
Example: 
DECLARE 
   greetings varchar2(30) := '......Great Learning.....'; 
BEGIN 
   dbms_output.put_line(RTRIM(greetings,'.')); 
   dbms_output.put_line(LTRIM(greetings, '.')); 
   dbms_output.put_line(TRIM( '.' from greetings)); 
END; 
/
Output:
......Great Learning  
Great Learning..... 
Great Learning  
PL/SQL procedure successfully completed.