Oracle useful functions - string manipulation, number calculations, date conversion and aggregation

Oracle provides many built-in functions that are useful for string manipulation, number calculations, date operations, and data aggregation. Here are some of the most commonly used Oracle functions:


🔹 String Functions

FunctionDescriptionExample
UPPER()Converts to uppercaseUPPER('oracle') → 'ORACLE'
LOWER()Converts to lowercaseLOWER('ORACLE') → 'oracle'
INITCAP()Capitalizes first letter of each wordINITCAP('hello world') → 'Hello World'
LENGTH()Returns length of stringLENGTH('Oracle') → 6
SUBSTR()Extracts substringSUBSTR('Oracle', 2, 3) → 'rac'
INSTR()Finds position of substringINSTR('Oracle', 'a') → 3
REPLACE()Replaces substringREPLACE('hello world', 'world', 'Oracle') → 'hello Oracle'
REGEXP_SUBSTR()Extracts substring using regexREGEXP_SUBSTR('123-456-789', '\d+', 1, 2) → '456'
LISTAGG()Aggregates multiple rows into a single stringLISTAGG(name, ', ') WITHIN GROUP (ORDER BY name)

🔹 Number Functions

FunctionDescriptionExample
ROUND()Rounds number to given decimal placesROUND(123.456, 2) → 123.46
TRUNC()Truncates number without roundingTRUNC(123.456, 2) → 123.45
MOD()Returns remainderMOD(10, 3) → 1
CEIL()Rounds up to nearest integerCEIL(4.2) → 5
FLOOR()Rounds down to nearest integerFLOOR(4.9) → 4
ABS()Returns absolute valueABS(-10) → 10
SIGN()Returns sign (-1, 0, 1)SIGN(-50) → -1
POWER()Raises number to a powerPOWER(2, 3) → 8

🔹 Date Functions

FunctionDescriptionExample
SYSDATEReturns current date and timeSELECT SYSDATE FROM DUAL;
SYSTIMESTAMPReturns current timestamp with timezoneSELECT SYSTIMESTAMP FROM DUAL;
ADD_MONTHS()Adds months to a dateADD_MONTHS(SYSDATE, 3)
MONTHS_BETWEEN()Finds difference between two dates in monthsMONTHS_BETWEEN('01-JAN-2024', '01-JUL-2024') → 6
LAST_DAY()Returns last day of monthLAST_DAY(SYSDATE)
NEXT_DAY()Returns next given weekdayNEXT_DAY(SYSDATE, 'Monday')
TRUNC()Truncates date to specific unitTRUNC(SYSDATE, 'MONTH')
EXTRACT()Extracts part of dateEXTRACT(YEAR FROM SYSDATE) → 2024

🔹 Conversion Functions

FunctionDescriptionExample
TO_CHAR()Converts date/number to stringTO_CHAR(SYSDATE, 'DD-MON-YYYY')
TO_DATE()Converts string to dateTO_DATE('2024-01-01', 'YYYY-MM-DD')
TO_NUMBER()Converts string to numberTO_NUMBER('12345')

🔹 Aggregate Functions

FunctionDescriptionExample
SUM()Returns sum of valuesSELECT SUM(salary) FROM employees;
AVG()Returns averageSELECT AVG(salary) FROM employees;
MIN()Returns minimum valueSELECT MIN(salary) FROM employees;
MAX()Returns maximum valueSELECT MAX(salary) FROM employees;
COUNT()Returns count of rowsSELECT COUNT(*) FROM employees;

🔹 Analytical Functions

FunctionDescriptionExample
RANK()Assigns rank with gapsRANK() OVER (ORDER BY salary DESC)
DENSE_RANK()Assigns rank without gapsDENSE_RANK() OVER (ORDER BY salary DESC)
ROW_NUMBER()Assigns unique row numberROW_NUMBER() OVER (ORDER BY salary DESC)
LEAD()Gets next row's valueLEAD(salary) OVER (ORDER BY salary)
LAG()Gets previous row's valueLAG(salary) OVER (ORDER BY salary)

🔹 Miscellaneous Functions

FunctionDescriptionExample
NVL()Replaces NULL with a valueNVL(NULL, 'Default') → 'Default'
NVL2()Returns different values for NULL/non-NULLNVL2(salary, 'Present', 'Missing')
DECODE()IF-THEN-ELSE alternativeDECODE(status, 'A', 'Active', 'I', 'Inactive', 'Unknown')
CASEMore flexible conditional logicCASE WHEN status = 'A' THEN 'Active' ELSE 'Inactive' END

🔹 Regular Expression Functions (Regex)

FunctionDescriptionExample
REGEXP_LIKE()Checks if value matches patternREGEXP_LIKE('123ABC', '^[0-9]+$') → FALSE
REGEXP_INSTR()Finds position of regex patternREGEXP_INSTR('abc123xyz', '\d+') → 4
REGEXP_REPLACE()Replaces regex matchREGEXP_REPLACE('a1b2c3', '\d', '') → 'abc'
REGEXP_SUBSTR()Extracts matching substringREGEXP_SUBSTR('a1b2c3', '\d+') → '1'

✨ Summary

🔹 String FunctionsUPPER, SUBSTR, LISTAGG, REGEXP_REPLACE
🔹 Number FunctionsROUND, MOD, POWER
🔹 Date FunctionsSYSDATE, LAST_DAY, EXTRACT
🔹 Conversion FunctionsTO_CHAR, TO_DATE, TO_NUMBER
🔹 Aggregate FunctionsSUM, AVG, COUNT
🔹 Analytical FunctionsRANK, ROW_NUMBER, LEAD
🔹 Miscellaneous FunctionsNVL, DECODE, CASE

Post a Comment

And that's all there is to it!

If anyone has any other questions or requests for future How To posts, you can either ask them in the comments or email me. Please don't feel shy at all!

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

Previous Post Next Post