- Concatenate strings with
||and use the string functions. - Combine INSTR and SUBSTR to extract parts of a string.
- Explain Oracle’s automatic type conversion rules.
- Use TO_CHAR, TO_DATE and TO_NUMBER with format models.
- Use DECODE, GREATEST, LEAST and NVL.
String Functions
Strings are stored in CHAR and VARCHAR2 columns. Two strings are joined with the concatenation operator ||: select name || ' ' || qual from faculty;
| Function | Description |
|---|---|
| LENGTH(string) | Number of characters |
| LOWER / UPPER(string) | Convert to lowercase / uppercase |
| INITCAP(string) | First letter of every word uppercase, rest lowercase |
| LPAD / RPAD(string, len [, fill]) | Pad on the left / right to len (default fill = space); truncates longer strings |
| LTRIM / RTRIM(string [, set]) | Remove characters in set (default space) from the left / right until a character not in the set |
| TRIM(string) | Remove leading and trailing spaces (Oracle8i) |
| SUBSTR(string, pos [, len]) | Extract len characters from pos (to the end if len is omitted) |
| INSTR(s1, s2 [, pos [, occurrence]]) | Starting position of s2 in s1; 0 if not found |
| ASCII(string) / CHR(number) | ASCII code of the first character / character for a code |
| TRANSLATE(string, from, to) | Character-by-character substitution |
| REPLACE(string, source, replace) | Replace every occurrence of a substring |
select name, duration from courses
where lower(name) like '%programming%'; -- case-insensitive search
select initcap('this IS to Test INITCAP') result from dual; -- This Is To Test Initcap
select instr('How do you do','do') from dual; -- 5 (first occurrence)
select instr('How do you do','do',8) from dual; -- 12 (search from position 8)
select instr('How do you do','do',1,2) from dual; -- 12 (2nd occurrence)
select substr('Srikanth Technologies',10) from dual; -- Technologies
select name, substr(name, 1, instr(name,' ') - 1) firstname from faculty;
select name, substr(name, instr(name,' ') + 1) lastname from faculty;where instr(dp,'APR') <> 0.select ltrim('aabcbadxyabc','abc') from dual; -- dxyabc
select rpad(name,12,'.') from courses; -- VB.NET...... / Oracle datab
select replace ('ABC ABAC XYZ DABC','ABC','PQR') from dual; -- PQR ABAC XYZ DPQR
select translate('ABC ABAC XYZ DABC','ABC','PQR') from dual; -- PQR PQPR XYZ DPQRConversion Functions
| Function | Converts |
|---|---|
| TO_CHAR(value [, format]) | DATE or NUMBER → CHAR (and formats it) |
| TO_DATE(char [, format]) | CHAR → DATE |
| TO_NUMBER(char [, format]) | CHAR → NUMBER |
Automatic type conversion – guidelines
- Any NUMBER or DATE is converted to CHAR (e.g.
LENGTH(1133)). - CHAR becomes NUMBER only if it contains digits, a decimal point or a leading minus sign (
5 * '20'= 100). - CHAR becomes DATE only in DD-MON-YY or DD-MON-YYYY format (
LAST_DAY('20-aug-2001')). - A DATE is never converted to NUMBER and a NUMBER is never converted to DATE.
NEXT_DAY('12-1-2001','Fri') fails with ORA-01843: not a valid month – the month must be given as letters, otherwise use TO_DATE with a format.
Format elements (TO_CHAR / TO_DATE)
| Element | Meaning | Element | Meaning |
|---|---|---|---|
| MM | Month number (10) | YYYY / YY | 4- / 2-digit year |
| MON | First 3 letters (OCT) | YEAR | Year spelled out |
| MONTH | Full month name | HH / HH12, HH24 | Hour (12 / 24-hour) |
| DDD / DD / D | Day of year / month / week | MI, SS | Minutes, seconds |
| Day | Day name (Wednesday) | A.M. / P.M. | Meridian indicator |
| fm | Removes padding spaces (‘May ’ → ‘May’) | TH / SP | 16th / spelled out (DDSP) |
select to_char(sysdate,'dd-mm-yyyy') from dual;
select to_char(sysdate,'dd Month yyyy hh24:mi:ss') from dual;
select bcode, name from students where to_char(dj,'mmyyyy') = '042001';
select bcode, name, to_char(dj,'dd-fmMonth-yyyy') dj from students; -- no padding
insert into payments
values (10, to_date('14-04-2001 10:20:00','dd-mm-yyyy hh24:mi:ss'), 2000);
select to_number('$333','$999') * 20 from dual; -- 6660
select vno from vehicles order by to_number(vno); -- numeric sort of a VARCHAR2 column- The case of TO_CHAR output follows the format: Month → April, MONTH → APRIL. Month names are padded to 9 characters unless fm is used.
- With TO_DATE the value must match the format exactly, otherwise it may be misinterpreted.
- TO_NUMBER is needed only to convert a formatted number (e.g. with a currency symbol –
$333 * 20gives ORA-00911) and to sort numeric strings numerically.
Miscellaneous Functions
| Function | Description |
|---|---|
| DECODE(expr, v1, r1, v2, r2, ..., else) | Works like a multiple IF / CASE: returns rn for the first matching vn, otherwise else |
| GREATEST(v1, v2, ...) | Largest value (sets a lower limit) |
| LEAST(v1, v2, ...) | Smallest value (sets an upper limit) |
| NVL(v1, v2) | v2 if v1 is NULL, otherwise v1 |
select fcode, ccode,
decode(grade,'A','Very Good','B','Good','C','Average','Unknown') grade
from course_faculty;
select ccode, greatest(fee * 0.10, 500) discount from courses; -- at least 500
select ccode, least (fee * 0.10, 500) discount from courses; -- at most 500
select bcode, nvl(enddate, sysdate) - stdate nodays,
decode(enddate, null, 'Running', 'Completed') status
from batches;GREATEST('12-jun-2001','17-mar-2001') returns ‘17-mar-2001’.- || concatenates; INSTR finds, SUBSTR extracts; TRANSLATE works per character, REPLACE per substring.
- Oracle converts types automatically when the value already “looks like” the target type.
- TO_CHAR formats dates and numbers; TO_DATE needs a matching format.
- DECODE is an if-elsif-else; NVL replaces NULL.
Chapter 7 Notes (PDF)
Question / Answer (15 questions)
Moderate-level questions based on the Chapter 7 notes (PDF). Click a question to see the answer.
MCQ Test : Check Your Understanding (5 questions)
Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.
Bibliography/References
The chapter notes PDF is the course study material and is credited to its author below. The explanations, questions and MCQs on this page are original study aids written for this course from the topics covered in that PDF. The other works are cited for verification and further reading. Oracle, SQL*Plus and PL/SQL are trademarks of Oracle Corporation; other names belong to their respective owners.
- Srikanth, P. (n.d.). Oracle For Beginners (chapter notes – the PDF shown on each chapter page). Srikanth Technologies. http://www.srikanthtechnologies.com/
- Oracle Corporation. (n.d.). Oracle Database SQL Language Reference, 19c. https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/
- Oracle Corporation. (n.d.). Oracle Database Concepts, 19c. https://docs.oracle.com/en/database/oracle/oracle-database/19/cncpt/
- Feuerstein, S., & Pribyl, B. (2014). Oracle PL/SQL Programming (6th ed.). O’Reilly Media.