String in Pl/SQL
String in Pl/SQL
1. What is String?
- Definition : A sequence of characters
- The characters could be numeric, letters, blank, special characters or a combination of all.
2. Question: Print "Hello World" in PL/SQL.
Program 1:
DECLARE
str VARCHAR2(25);
BEGIN
str:= 'Hello World';
dbms_output.put_line (str);
END;
/
Output: Hello World
Note : Single quotes are used to indicate the beginning and end of a string in SQL. Double quotes generally aren't used in SQL but vary from database to database.
Program 2:
DECLARE
str VARCHAR2(25);
BEGIN
str:= "Hello World";
dbms_output.put_line (str);
END;
/
Output:
ERROR at line 4:
ORA-06550: line 4, column 8:
PLS-00201: identifier 'Hello World' must be declared
ORA-06550: line 4, column 1:
PL/SQL: Statement ignored
3. Write a PL/SQL code to print ASCII value of string in PL/SQL.
Program 1:
DECLARE
str VARCHAR2(25);
BEGIN
str:= 'A';
dbms_output.put_line (ASCII(str));
END;
/
Output : 65
Program 2:
DECLARE
str VARCHAR(25);
result INTEGER;
BEGIN
str:='a';
result:=ASCII(str);
dbms_output.put_line(result);
END;
/
Program 3:
DECLARE
str VARCHAR2(25);
BEGIN
str:= 'Hello World';
dbms_output.put_line (ASCII(str));
END;
/
Output : 72
Because it will take first character : H
4. Write a PL/SQL code to convert ASCII value to Character.
Program 2:
--To covert number to ASCII Value
BEGIN
dbms_output.put_line (CHR(65));
END;
/
Output : A
5. Write a PL/SQL code to append/concat the string.
Syntax : concat(str1,str2)
Program :
--To append/concat the string in pl/sql
DECLARE
str1 VARCHAR2(25);
str2 VARCHAR2(25);
BEGIN
str1 := 'Hello';
str2 := 'World';
dbms_output.put_line (CONCAT(str1,str2));
END;
/
Output : Hello World
6. Write a PL/SQL code to convert the first character in uppercase .
Syntax : concat(str1,str2)
Program :
--To convert the first character in uppercase
DECLARE
str1 VARCHAR2(25);
BEGIN
str1 := 'hello';
dbms_output.put_line (INITCAP(str1));
END;
/
Output:
Hello
7. Write a PL/SQL code to convert the string in uppercase.
Program :
--To convert the string in uppercase
DECLARE
str1 VARCHAR2(25);
BEGIN
str1 := 'hello';
dbms_output.put_line (upper(str1));
END;
/
Output : HELLO
8. Write a PL/SQL code to count the string .
Program :
--To count the string
DECLARE
str1 VARCHAR2(25);
BEGIN
str1 := 'HELLO';
dbms_output.put_line (length(str1));
END;
/
Output : 5
9. Write a PL/SQL code to retrieve the remainder of the string starting from the third position.
Program:
--Retrieve the remainder of the string starting from the third position.
DECLARE
str1 VARCHAR2(25);
BEGIN
str1 := 'HELLO';
dbms_output.put_line ( SUBSTR (str1, 3));
END;
/
Output: LLO
Comments
Post a Comment