You are on page 1of 7

Oracle Single Row Functions

Oracle provides single row functions to manipulate the data values. The single row functions operate on single rows and return only one result per row. In general, the functions take one or more inputs as arguments and return a single value as output. The arguments can be a usersupplied constant, variable, column name and an expression. The features of single row functions are:

Act on each row returned in the query. Perform calculations on data. Modify the data items. Manipulate the output for groups of rows. Format numbers and dates. Converts column data types. Returns one result per row. Used in SELECT, WHERE and ORDER BY clauses. Single row functions can be nested.

The single row functions are categorized into


Character Functions: Character functions accept character inputs and can return either character or number values as output. Number Functions: Number functions accepts numeric inputs and returns only numeric values as output. Date Functions: Date functions operate on date data type and returns a date value or numeric value. Conversions Functions: Converts from one data type to another data type. General Functions

Let see each function with an example:

Character Functions Example


Character Function Summary

Function

Description ASCII Returns the ASCII decimal equivalent of a character CHR Returns the character given the decimal equivalent CONCAT Concatenates two strings; same as the operator || INITCAP Returns the string with the first letter of each word in uppercase INSTR Finds the numeric starting position of a string within a string INSTRB Same as INSTR but counts bytes instead of characters LENGTH Returns the length of a string in characters LENGTHB Returns the length of a string in bytes LOWER Converts a string to all lowercase LPAD Left-fills a string to a set length using a specified character LTRIM Strips leading characters from a string REPLACE Performs substring search and replace RPAD Right-fills a string to a set length using a specified character RTRIM Strips trailing characters from a string SOUNDEX Returns a phonetic representation of a string SUBSTR Returns a section of the specified string, specified by numeric character positions SUBSTRB Returns a section of the specified string, specified by numeric byte positions TRANSLATE Performs character search and replace TRIM Strips leading, trailing, or both leading and trailing characters from a string UPPER Converts a string to all uppercase 1. LOWER The Lower function converts the character values into lowercase letters.
SELECT lower('ORACLE') FROM DUAL;

2. UPPER The Upper function converts the character values into uppercase letters.
SELECT upper('oracle') FROM DUAL;

3. INITCAP The Initcap function coverts the first character of each word into uppercase and the remaining characters into lowercase.
SELECT initcap('LEARN ORACLE') FROM DUAL;

4. CONCAT The Concat function coverts the first string with the second string.
SELECT concat('Oracle',' Backup') FROM DUAL;

5. SUBSTR The Substr function returns specified characters from character value starting at position m and n characters long. If you omit n, all characters starting from position m to the end are returned.
Syntax: substr(string [,m,n]) SELECT substr('ORACLE DATA RECOVERY',8,4) FROM DUAL; SELECT substr('ORACLE DATA PUMP',8) FROM DUAL;

You can specify m value as negative. In this case the count starts from the end of the string.
SELECT substr('ORACLE BACKUP',-6) FROM DUAL;

6. LENGTH The Length function is used to find the number of characters in a string.
SELECT length('Oracle Data Guard') FROM DUAL;

7. INSTR The Instr function is used to find the position of a string in another string. Optionally you can provide position m to start searching for the string and the occurrence n of the string. By default m and n are 1 which means to start the search at the beginning of the search and the first occurrence.
Syntax: instr('Main String', 'substring', [m], [n]) SELECT instr('oralce apps','app') FROM DUAL; SELECT instr('oralce apps is a great application','app',1,2) FROM DUAL;

8. LPAD The Lpad function pads the character value right-justified to a total width of n character positions.
Syntax: lpad(column, n, 'string'); SELECT lpad('100',5,'x') FROM DUAL;

9. RPAD The Rpad function pads the character value left-justified to a total width of n character positions.
Syntax: rpad(column, n, 'string'); SELECT rpad('100',5,'x') FROM DUAL;

10. TRIM The Trim function removes the leading or trailing or both the characters from a string.
Syntax: trim(leading|trailing|both, trim_char from trim_source) SELECT trim('O' FROM 'ORACLE') FROM DUAL;

11. REPLACE The Replace function is used to replace a character with another character in a string.
Syntax: replace(column, old_char,new_char) SELECT replace('ORACLE DATA BACKUP', 'DATA','DATABASE') FROM DUAL;

Number Functions Example


1. ROUND The Round function rounds the value to the n decimal values. If n is not specified, there won't be any decimal places. If n is negative, numbers to the left of the decimal point are rounded.
Syntax: round(number,n) SELECT round(123.67,1) FROM DUAL; SELECT round(123.67) FROM DUAL; SELECT round(123.67,-1) FROM DUAL;

2. TRUNC The Trunc function truncates the value to the n decimal places. If n is omitted, then n defaults to zero.
Syntax: trunc(number,n) SELECT trunc(123.67,1) FROM DUAL; SELECT trunc(123.67) FROM DUAL;

3. MOD The Mod function returns the remainder of m divided by n.


Syntax: mod(m,n) SELECT mod(10,5) FROM DUAL;

Date Functions Example


1. SYSDATE The Sysdate function returns the current oracle database server date and time.
SELECT sysdate FROM DUAL;

2. Arithmetic with Dates You can add or subtract the number of days or hours to the dates. You can also subtract the dates
SELECT sysdate+2 "add_days" FROM DUAL; SELECT sysdate-3 "sub_days" FROM DUAL; SELECT sysdate+3/24 "add_hours" FROM DUAL; SELECT sysdate-2/24 "sub_hours" FROM DUAL; SELECT sysdate-hiredate "sub_dates" FROM EMP; -- returns number of days between the two dates.

3. MONTHS_BETWEEN The Months_Between function returns the number of months between the two given dates.
Syntax: months_between(date1,date2) SELECT months_between(sysdate,hiredate) FROM EMP; SELECT months_between('01-JUL-2000', '23-JAN-2000') FROM DUAL;

4. ADD_MONTHS The Add_Months is used to add or subtract the number of calendar months to the given date.
Syntax: add_months(date,n) SELECT add_months(sysdate,3) FROM DUAL; SELECT add_months(sysdate,-3) FROM DUAL; SELECT add_months('01-JUL-2000', 3) FROM DUAL;

5. NEXT_DAY The Next_Day function finds the date of the next specified day of the week. The syntax is NEXT_DAY(date,'char') The char can be a character string or a number representing the day.
SELECT next_day(sysdate,'FRIDAY') FROM DUAL; SELECT next_day(sysdate,5) FROM DUAL; SELECT next_day('01-JUL-2000', 'FRIDAY') FROM DUAL;

6. LAST_DAY The Last_Day function returns the last day of the month.
SELECT last_day(sysdate) FROM DUAL; SELECT last_day('01-JUL-2000') FROM DUAL;

7. ROUND The Round function returns the date rounded to the specified format. The Syntax is Round(date [,'fmt'])
SELECT round(sysdate,'MONTH') FROM DUAL; SELECT round(sysdate,'YEAR') FROM DUAL;

8. TRUNC The Trunc function returns the date truncated to the specified format. The Syntax is Trunc(date [,'fmt'])
SELECT trunc(sysdate,'MONTH') FROM DUAL; SELECT trunc(sysdate,'YEAR') FROM DUAL;

9. TRANSLATE
TRANSLATE (c1, c2, c3) takes three arguments, where c1, c2, and c3 are character strings. This function returns c1 with all occurrences of characters in c2 replaced with the positionally corresponding characters in c3. A NULL is returned if any of c1, c2, or c3 is NULL. If c3 has fewer characters than c2, the unmatched characters in c2 are removed from c1. If c2 has fewer characters than c3, the unmatched characters in c3 are ignored. TRANSLATE is similar to the REPLACE function. REPLACE substitutes a single string from another string, whereas TRANSLATE makes several single-character one-to-one substitutions. The following example substitutes * for a, # for e, and $ for i, and it removes o and u from the ename column: Select ename, translate(ename, 'aeiou', '*#$') no_vowel from emp where ename like 'S%'; ENAME ---------SMITH SCOTT NO_VOWEL ---------SMITH SCOTT

Here is another example, where the case is reversed; uppercase letters are converted to lowercase, and lowercase letters are converted to uppercase: Select ename, translate(ename,
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz') Name from emp;

ENAME ---------SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILLER

NAME ------smith allen ward jones martin blake clark scott king turner adams james ford miller

10. SOUNDEX takes a single argument, where c1 is a character string. This function returns the Soundex phonetic representation of c1. The SOUNDEX function is usually used to locate names that sound alike. The example returns the records with first names that sound like " Jones".
SOUNDEX(c1) SELECT ename FROM emp WHERE SOUNDEX(ename) = SOUNDEX(Jones); ENAME ---------JONES JAMES

You might also like