Q: | |
18-1. | The CONCAT function or the concatenation operator does the trick. The CONCAT function has two string arguments that it returns concatenated. The concatenation operator is specified as || and takes its arguments from its left and right sides. You may be interested to know that || is actually a function that is itself declared in the STANDARD package: DECLARE
first_three VARCHAR2(3) := 'ABC';
second_three VARCHAR2(3) := 'DEF';
third_three VARCHAR2(3) := 'GHI';
whole_thing VARCHAR2(9);
BEGIN
/*
|| The CONCAT function requires multiple steps to
|| concatenate multiple strings
*/
whole_thing := CONCAT(first_three,second_three);
whole_thing := CONCAT(whole_thing,third_three);
DBMS_OUTPUT.PUT_LINE(whole_thing);
/*
|| The concatenation operator concatenates multiple
|| very quick and easy
*/
whole_thing := first_three || second_three || third_three;
DBMS_OUTPUT.PUT_LINE(whole_thing);
END; Note also that both functions perform an on-the-fly conversion of their arguments to VARCHAR2 datatypes: SQL> EXEC DBMS_OUTPUT.PUT_LINE (CONCAT(1,2))
12
SQL> EXEC DBMS_OUTPUT.PUT_LINE (1 || 2)
12 Remember that if the value to be converted is of the date datatype, National Language Support (NLS) settings will affect the appearance of the resulting string. |
Q: | |
18-2. | The two words “in string” will lead you directly to the aptly named INSTR function. This function has two obvious arguments (the string to search and the string to search for). It also has two, not so obvious, but very powerful arguments (where to start and what occurrence to find): DECLARE
old_blue_eyes VARCHAR2(14) := 'do be do be do';
v_temp VARCHAR2(10);
BEGIN
/*
|| Find the first do three times
*/
v_temp:= INSTR(old_blue_eyes,'do',1,1);
v_temp:= INSTR(old_blue_eyes,'do',1);
v_temp:= INSTR(old_blue_eyes,'do'),
/*
|| Find the second do by looking for the
|| second occurrence
*/
v_temp := INSTR(old_blue_eyes,'do',1,2);
/*
|| Find the second do by looking for the
|| first occurrence after the third character
*/
v_temp := INSTR(old_blue_eyes,'do',3,1);
/*
|| Find the very last do (twice)
*/
v_temp := INSTR(old_blue_eyes,'do',1,3);
v_temp := INSTR(old_blue_eyes,'do',10,1);
END; The INSTR function is also quite good about converting its arguments to VARCHAR2: SQL> EXEC DBMS_OUTPUT.PUT_LINE (INSTR ( 1234567890,4));
4 Be careful with case, though; INSTR is not so forgiving in that regard. This example forever returns a big fat zero: SQL> EXEC DBMS_OUTPUT.PUT_LINE ( INSTR( 'Looter!','l'));
0 |
Q: | |
18-3. | The REPLACE function can substitute sections of a string, or the TRANSLATE function can substitute character by character. Both functions have the same three arguments: a string, a string to search for, and a string to replace. They work in different ways, though. Use REPLACE when you want to substitute every occurrence of one string in another: SQL> EXEC DBMS_OUTPUT.PUT_LINE
(REPLACE ('Rain, Rain and More Rain','Rain','Sunshine'));
Sunshine, Sunshine and More Sunshine Use TRANSLATE to substitute every occurrence of single characters in a string. For example, this code substitutes A for C, and C for A, to return CCCBAAA: SQL> EXEC DBMS_OUTPUT.PUT_LINE(TRANSLATE('AAABCCC','AC','CA'));
CCCBAAA |
Q: | |
18-4. | Since the number of characters in a string is equal to its length, the LENGTH function applies here: v_string_length := LENGTH(v_the_string); Don’t forget that a blank space is considered a character: LENGTH('Hi There') <> LENGTH(' Hi There ') |
Q: | |
18-5. | Here’s the output: SQL> EXEC DBMS_OUTPUT.PUT_LINE(LENGTH(1 * 100));
3 3 is the number of characters in 100 (100 * 1). Note that the LENGTH function is quite gracious about converting numeric values to VARCHAR2. |
Q: | |
18-6. | Here’s the code: UPDATE employee
SET last_name = UPPER(last_name)
WHERE last_name != UPPER(last_name); |
Q: | |
18-7. | Why, UPPER and LOWER, of course. These functions change the case of the whole string and are useful for making case-neutral comparisons in code, for example: IF UPPER('Procurement') = 'PROCUREMENT'... |
Q: | |
18-8. | Here’s a suggested block: BEGIN
/*
|| The handy dandy SUBSTR function does the trick with
|| its last two arguments, start location and length.
|| Here we say start at the fourth character and show
|| it plus the next two.
*/
DBMS_OUTPUT.PUT_LINE(SUBSTR('BRUSHLOTS',4,3));
END; |
Q: | |
18-9. | A string can be padded on the left side or the right side by LPAD and RPAD, respectively. Each of these functions returns the padded string specified by three arguments: the string to pad, the length to pad to, and the characters to pad with (a space by default). DECLARE
v_temp VARCHAR2(30);
BEGIN
v_temp := RPAD('Pad with blanks',30);
v_temp := RPAD('Pad with stars',30,'*'),
v_temp := RPAD('Pad with stars and tildes',30,'*~'),
v_temp := RPAD('Will be truncated at 3',3,'*'),
END; |
Q: | |
18-10. | The INITCAP function does the trick here to change the initial letter of every word to capitals: SQL> EXEC DBMS_OUTPUT.PUT_LINE(INITCAP('this is a headline'));
This Is A Headline |
Q: | |
18-11. | The LIKE function in SQL can eliminate the unwanted rows from the table. Then all that is required is to loop through each record in the query: DECLARE
CURSOR C1 IS SELECT * FROM EMPLOYEE WHERE UPPER(LAST_NAME) LIKE '%E%';
BEGIN
FOR C1_REC IN C1 LOOP
DBMS_OUTPUT.PUT_LINE(C1_REC.LAST_NAME||', '||C1_REC.FIRST_NAME);
END LOOP;
END; |
Q: | |
18-12. | The INSTR function can calculate this number. Recall that one of the parameters INSTR accepts is the occurrence of the string you wish to find. By using this, you need only place a single call to INSTR to satisfy the requirement: SQL> EXEC DBMS_OUTPUT.PUT_LINE (instr(upper('Steven Feuerstein'),'E', 1, 2));
5 |
Q: | |
18-13. | Here’s the block: BEGIN
/*
|| Utilize the little known but very effective ability to start searching
|| from the end of the string by specifying a negative third argument for
|| the INSTR function.
||
|| I must confess that I have written many a function to do just this
|| sort of thing in the past without realizing INSTR could do it on its
|| own (DH)
*/
DBMS_OUTPUT.PUT_LINE(INSTR(UPPER('Steven Feuerstein'),'E',-1,3));
END; |
Q: | |
18-14. | The LTRIM function does this nicely by specifying that all zeros be removed from the left side of the string as follows: LTRIM(my_formatted_number,'0') |
Q: | |
18-15. | Hmm, what function is used to left-pad a string? Why, LPAD of course! And since you know that blanks are considered characters (as per a string’s length), write the following: /* we also know that blanks are the default padding character */
v_number_string := LPAD(v_number_string,15); |
Q: | |
18-16. | This does the trick: DECLARE
v_new_name VARCHAR2(50);
BEGIN
/*
|| Because we are only replacing a single character the
|| REPLACE or TRANSLATE function will suffice
*/
v_new_name := REPLACE('Steven Feuerstein','e','z'),
v_new_name := TRANSLATE('Steven Feuerstein','e','z'),
END; |
Q: | |
18-17. | Here you go: DECLARE
v_new_name VARCHAR2(50);
BEGIN
/*
|| The replace function can be used to substitute nothing
|| for a string
*/
v_new_name := REPLACE('Steven Feuerstein','e',''),
END; |
Q: | |
18-18. | Try this replacement: v_new_string :=
REPLACE('IF TO_DATE (my_date_str, ''MM-DD-YY'') > SYSDATE','YY','RR'), |
Q: | |
18-19. | This code performs the replacement: v_new_string := REPLACE(UPPER('IF TO_DATE (my_date_str,''MM-
DD-YY'') > TO_DATE (your_date_str,''mmddyy'')'),'YY','RR'), |
Q: | |
18-20. | The RPAD command shown here accomplishes what you need by padding the string “-” using the option parameter to specify the pad character of “-”: RPAD ('-', 80, '-') |