The Programmer's Guide
  • About
  • Algorithm
    • Big O Notation
      • Tree
      • Problems
    • Basic Notes
    • Data Structure Implementation
      • Custom LinkedList
      • Custom Stack
      • Custom Queue
      • Custom Tree
        • Binary Tree Implementation
        • Binary Search Tree Implementation
        • Min Heap Implementation
        • Max Heap Implementation
        • Trie Implementation
      • Custom Graph
        • Adjacency List
        • Adjacency Matrix
        • Edge List
        • Bidirectional Search
    • Mathematical Algorithms
      • Problems - Set 1
      • Problems - Set 2
    • Bit Manipulation
      • Representation
      • Truth Tables
      • Number System
        • Java Program
      • Problems - Set 1
    • Searching
    • Sorting
    • Array Algorithms
    • String Algorithms
    • Tree
      • Tree Traversal Techniques
      • Tree Implementation
      • Applications of Trees
      • Problems - Set 1
    • Graph
      • Graph Traversal Techniques
      • Shortest Path Algorithms
      • Minimum Spanning Tree (MST) Algorithms
    • Dynamic Programming
      • Problems - Set 1
    • Recursion
    • Parallel Programming
    • Miscellaneous
      • Problems - Set 1
  • API
    • API Basics
      • What is an API?
      • Types of API
        • Comparison - TBU
      • Synchronous vs Asynchronous API
    • API Architecture
      • Synchronous & Asynchronous Communication
    • API Specification
  • Cloud Computing
    • Cloud Fundamentals
      • Cloud Terminology
      • Core Terminology
      • Cloud Models
      • Cloud Service Models
      • Benefits, Challenges and Risk of Cloud Computing
      • Cloud Ecosystem
  • Database
    • DBMS
      • Types of DBMS
        • Relational DBMS (RDBMS)
        • NoSQL DBMS
        • Object-Oriented DBMS (OODBMS)
        • Columnar DBMS
        • In-Memory DBMS
        • Distributed DBMS
        • Cloud-Based DBMS
        • Hierarchical DBMS
      • DBMS Architecture
      • DBMS Structure
    • SQL Databases
      • Terminology
      • RDBMS Concepts
        • Entity Relationship Diagram (ERD)
          • ERD Examples
        • Normalization
        • Denormalization
        • ACID & BASE Properties
          • ACID Properties
          • BASE Properties
        • Locking and Unlocking
      • SQL Fundamentals
        • SQL Commands
          • DDL (Data Definition Language)
          • DML (Data Manipulation Language)
          • DCL (Data Control Language)
          • TCL (Transaction Control Language)
          • DQL (Data Query Language)
        • SQL Operators
          • INTERSECT
          • EXCEPT
          • MINUS
          • IN and NOT IN
          • EXISTS and NOT EXISTS
        • SQL Clauses
          • Joins
          • OVER
          • WITH
          • CONNECT BY
          • MODEL
          • FETCH FIRST
          • KEEP
          • OFFSET with FETCH
        • SQL Functions
          • Oracle Specific
        • SQL Data Types
          • Numeric Types
          • Character Types
          • Date & Time Types
          • Large Object Types
        • Others
          • Indexing
      • Vendor Specific Concepts
        • Oracle Specific
          • Data Types
          • Character Set
          • Rownum, Rowid, Urowid
          • Order of Execution of the query
          • Keys
          • Tablespace
          • Partition
      • Best Practice
      • Resources & References
        • O’Reilly SQL Cookbook (2nd Edition)
          • 1. Retrieving Records
          • 2. Sorting Query Results
          • 3. Working with Multiple Tables
          • 4. Inserting, Updating, and Deleting
          • 5. Metadata Queries
          • 6. Working with Strings
          • 7. Working with Numbers
          • 8. Date Arithmetic
          • 9. Date Manipulation
          • 10. Working with Ranges
          • 11. Advanced Searching
          • 12. Reporting and Reshaping
          • 13. Hierarchical Queries
          • 14. Odds 'n' Ends
    • SQL vs NoSQL
    • Best Practices
  • Git
    • Commands
      • Setup and Configuration Commands
      • Getting and Creating Projects
      • Tracking Changes
      • Branching and Merging
      • Sharing and Updating Projects
      • Inspection and Comparison
      • Debugging
      • Patching
      • Stashing and Cleaning
      • Advanced Manipulations
    • Workflows
      • Branching Strategies
        • Git Flow
        • Trunk-Based Development
        • GitHub Flow
        • Comparison
      • Merge Strategies
        • Merge
        • Rebase
        • Squash
        • Fast-forward vs No-fast-forward
        • MR vs PR
      • Conflict Resolution
        • Handling Merge Conflicts
        • Merge Conflicts
        • Rebase Conflicts
        • Divergent Branches After git pull
        • Force Push
      • Patch & Recovery
        • Cherry-pick strategies
        • Revert vs Reset
        • Recover from a bad rebase
      • Rebasing Practices
        • Merge vs Rebase
        • Rebase develop branch on main branch
      • Repository Management
        • Working Directory
        • Mirror a repository
        • Convert a local folder to a Git repo
        • Backup and restore a Git repository
  • Java
    • Java Installation
    • Java Distributions
    • Java Platform Editions
      • Java SE
      • Java EE
      • Jakarta EE
      • Java ME
      • JavaFX
    • Java Overview
      • OOP Principles
        • Encapsulation
        • Inheritance
        • Polymorphism
        • Abstraction
          • Abstract Class & Method
          • Interface
            • Functional Interfaces
            • Marker Interfaces
          • Abstract Class vs Interface
      • OOP Basics
        • What is a Class?
          • Types of Classes
        • What is an Object?
          • Equals and HashCode
            • FAQ
          • Shallow Copy and Deep Copy
          • Ways to Create Object
          • Serialization & Deserialization
        • Methods & Fields
          • Method Overriding & Overloading
          • Method Signature & Header
          • Variables
        • Constructors
        • Access Modifiers
      • Parallelism & Concurrency
        • Ways to Identify Thread Concurrency or Parallelism
        • Thread Basics
          • Thread vs Process
          • Creating Threads
          • Thread Context Switching
          • Thread Lifecycle & States
          • Runnable & Callable
          • Types of Threads
          • Thread Priority
        • Thread Management & Synchronisation
          • Thread Resource Sharing
          • Thread Synchronization
            • Why is Synchronization Needed?
            • Synchronized Blocks & Methods
          • Thread Lock
            • Types of Locks
            • Intrinsic Lock (Monitor Lock)
            • Reentrant Lock
          • Semaphore
          • Thread Starvation
          • Thread Contention
          • Thread Deadlock
          • Best Practices for Avoiding Thread Issues
      • Keywords
        • this
        • super
        • Access Modifiers
      • Data Types
        • Default Values
        • Primitive Types
          • byte
          • short
          • int
          • long
          • float
          • double
          • char
          • boolean
        • Non-Primitive (Reference) Types
          • String
            • StringBuilder
            • StringBuffer
              • Problems
            • Multiline String
            • Comparison - String, StringBuilder & StringBuffer
          • Array
          • Collections
            • List
              • Array vs List
              • ArrayList
              • Vector
                • Stack
                  • Problems
              • LinkedList
            • Queue
              • PriorityQueue
              • Deque (Double-Ended Queue)
                • ArrayDeque
                • ConcurrentLinkedDeque - TBU
                • LinkedBlockingDeque - TBU
            • Map
              • HashMap
              • Hashtable
              • LinkedHashMap
              • ConcurrentHashMap
              • TreeMap
              • EnumMap
              • WeakHashMap
            • Set
              • HashSet
              • LinkedHashSet
              • TreeSet
              • EnumSet
              • ConcurrentSkipListSet
              • CopyOnWriteArraySet
        • Specialized Classes
          • BigInteger
          • BigDecimal
            • Examples
          • BitSet
          • Date and Time
            • Examples
          • Optional
          • Math
          • UUID
          • Scanner
          • Formatter
            • Examples
          • Properties
          • Regex (Pattern and Matcher)
            • Examples
          • Atomic Classes
          • Random
          • Format
            • NumberFormat
            • DateFormat
            • DecimalFormat
        • Others
          • Object
          • Enum
            • Pre-Defined Enum
            • Custom Enum
            • EnumSet and EnumMap
          • Record
          • Optional
          • System
          • Runtime
          • ProcessBuilder
          • Class
          • Void
          • Throwable
            • Error
            • Exception
              • Custom Exception Handling
              • Best Practice
            • Error vs Exception
            • StackTraceElement
    • Java Features by Version
      • How New Java Features are Released ?
      • Java Versions
        • Java 8
        • Java 9
        • Scoped Values
        • Unnamed Variables & Patterns
      • FAQ
    • Concepts
      • Set 1
        • Streams
          • flatmap
          • Collectors Utility Class
          • Problems
        • Functional Interfaces
          • Standard Built-In Interfaces
          • Custom Interfaces
        • Annotation
          • Custom Annotation
          • Meta Annotation
        • Generics
          • Covariance and Invariance
        • Asynchronous Computation
          • Future
          • CompletableFuture
          • Future v/s CompletableFuture
          • ExecutorService
            • Thread Pool
            • Types of Work Queues
            • Rejection Policies
            • ExecutorService Implementations
            • ExecutorService Usage
          • Locks, Atomic Variables, CountDownLatch, CyclicBarrier - TBU
          • Parallel Streams, Fork/Join Framework,Stream API with Parallelism - TBU
      • Set 2
        • Standards
          • ISO Standards
          • JSR
            • JSR 303, 349, 380 (Bean Validation)
        • Operator Precedence
      • Set 3
        • Date Time Formatter
        • Validation
      • Set 4
        • Input from User
        • Comparison & Ordering
          • Object Equality Check
          • Comparable and Comparator
            • Comparator Interface
          • Sorting of Objects
          • Insertion Ordering
    • Packages
      • Core Packages
        • java.lang
          • java.lang.System
          • java.lang.Thread
      • Jakarta Packages
        • jakarta.validation
        • javax.validation
      • Third-party Packages
    • Code Troubleshoot
      • Thread Dump
      • Heap Dump
    • Code Quality & Analysis
      • ArchUnit
      • Terminologies
        • Cyclic dependencies
    • Code Style
      • Naming Convention
      • Package Structure
      • Formatting
      • Comments and Documentation
      • Imports
      • Exception Handling
      • Class Structure
      • Method Guidelines
      • Page 1
      • Code Smells to Avoid
      • Lambdas and Streams Style
      • Tools
    • Tools
      • IntelliJ IDEA
        • Shortcuts for MAC
      • Apache JMeter
        • Examples
      • Thread Dump Capture
        • jstack
        • VisualVM - TBU
        • jcmd - TBU
        • JConsole - TBU
        • YourKit Java Profiler - TBU
        • Eclipse MAT - TBU
        • IntelliJ IDEA Profiler - TBU
        • AppDynamics - TBU
        • Dynatrace - TBU
        • Thread Dump Analyzers - TBU
      • Heap Dump Capture
        • jmap
        • VisualVM - TBU
        • jcmd - TBU
        • Eclipse MAT (Memory Analyzer Tool) - TBU
        • IntelliJ IDEA Profiler - TBU
        • YourKit Java Profiler - TBU
        • AppDynamics - TBU
        • Dynatrace - TBU
        • Kill -3 Command - TBU
        • jhat (Java Heap Analysis Tool) - TBU
        • JVM Options - TBU
      • Wireshark
        • Search Filters
    • Best Practices
      • Artifact and BOM Versioning
  • Maven
    • Installation
    • Local Repository & Configuration
    • Command-line Options
    • Build & Lifecycle
    • Dependency Management
      • Dependency
        • Transitive Dependency
        • Optional Dependency
      • Dependency Scope
        • Maven Lifecycle and Dependency Scope
      • Dependency Exclusions & Overrides
      • Bill of Materials (BOM)
      • Dependency Conflict Resolution
      • Dependency Tree & Analysis
      • Dependency Versioning Strategies
    • Plugins
      • Build Lifecycle Management
      • Dependency Management
      • Code Quality and Analysis
      • Documentation Generation
      • Code Generation
      • Packaging and Deployment
      • Reporting
      • Integration and Testing
      • Customization and Enhancement
        • build-helper-maven-plugin
        • properties-maven-plugin
        • ant-run plugin
        • exec-maven-plugin
        • gmavenplus-plugin
      • Performance Optimization
    • FAQs
      • Fixing Maven SSL Issues: Unable to Find Valid Certification Path
  • Spring
    • Spring Basics
      • What is Spring?
      • Why Use Spring
      • Spring Ecosystem
      • Versioning
      • Setting Up a Spring Project
    • Core Concepts
      • Spring Core
        • Dependency Injection (DI)
        • Stereotype Annotation
      • Spring Beans
        • Bean Lifecycle
        • Bean Scope
          • Singleton Bean
        • Lazy & Eager Initialization
          • Use Case of Lazy Initialization
        • BeanFactory
        • ApplicationContext
      • Spring Annotations
        • Spring Boot Specific
        • Controller Layer (Web & REST Controllers)
    • Spring Features
      • Auto Configuration
        • Spring Boot 2: spring.factories
        • Spring Boot 3: spring.factories
      • Spring Caching
        • In-Memory Caching
      • Spring AOP
        • Before Advice
        • After Returning Advice
        • After Throwing Advice
        • After (finally) Advice
        • Around Advice
      • Spring File Handling
      • Reactive Programming
        • Reactive System
        • Reactive Stream Specification
        • Project Reactor
          • Mono & Flux
      • Asynchronous Computation
        • @Async annotation
      • Spring Security
        • Authentication
          • Core Components
            • Security Filter Chain
              • HttpSecurity
              • Example
            • AuthenticationManager
            • AuthenticationProvider
            • UserDetailsService
              • UserDetails
              • PasswordEncoder
            • SecurityContext
            • SecurityContextHolder
            • GrantedAuthority
            • Security Configuration (Spring Security DSL)
          • Authentication Models
            • One-Way Authentication
            • Mutual Authentication
          • Authentication Mechanism
            • Basic Authentication
            • Form-Based Authentication
            • Token-Based Authentication (JWT)
            • OAuth2 Authentication
            • Multi-Factor Authentication (MFA)
            • SAML Authentication
            • X.509 Certificate Authentication
            • API Key Authentication
            • Remember-Me Authentication
            • Custom Authentication
          • Logout Handling
        • Authorization
        • Security Filters and Interceptors
        • CSRF
          • Real-World CSRF Attacks & Prevention
        • CORS
        • Session Management and Security
        • Best Practices
      • Spring Persistence
        • JDBC
          • JDBC Components
          • JDBC Template
          • Transaction Management
          • Best Practices in JDBC Usage
          • Datasource
            • Connection Pooling
              • HikariCP
            • Caching
        • JPA (Java Persistence API)
          • JPA Fundamentals
          • ORM Mapping Annotations
            • 1. Entity and Table Mappings
            • 2. Field/Column Mappings
            • 3. Relationship Mappings
            • 4. Inheritance Mappings
            • 5. Additional Configuration Annotations
          • Querying Data
            • JPQL
            • Criteria API
            • JPA Specification
              • Example - Employee Portal
            • Native SQL Queries
            • Named Queries
            • Query Return Types
            • Pagination & Sorting
              • Example - Employee Portal
            • Projection
          • Fetch Strategies in JPA
        • JPA Implementation
          • Hibernate
            • Properties
            • Example
        • Spring Data JPA
          • Repository Abstractions
          • Entity-to-Table Mapping
          • Derived Query Methods
        • Cross-Cutting Concerns
          • Transactions
          • Caching
          • Concurrency
        • Examples
          • Employee Portal
            • API
    • Distributed Systems & Communication
      • Distributed Scheduling
      • Inter-Service Communication
        • 1. RestTemplate
        • 2. WebClient
        • 3. OpenFeign
        • Retry Mechanism
          • @Retryable annotation
            • Example
    • Security & Data Protection
      • Encoding | Decoding
        • Types
          • Base Encoding
            • Base16 - TBD
              • Encoding and Decoding in Java - TBD
            • Base32
              • Encoding and Decoding in Java
            • Base64 -TBD
              • Encoding and Decoding in Java - TBD
          • Text Encoding - TBD
            • Extended ASCII
              • Encoding and Decoding in Java - TBD
                • ISO-8859-1
                • Windows-1252 - TBD
                • IBM Code Pages - TBD
            • ASCII
              • Encoding and Decoding in Java
        • Java Guidelines
          • Text Encoding Decoding Examples
          • Base Encoding Decoding Examples
          • Best Practices and Concepts
          • Libraries
      • Cryptography
        • Terminology
        • Java Cryptography Architecture (JCA)
        • Key Management
          • Key Generation
            • Tools and Libraries
              • OpenSSL
              • Java Keytool
                • Concept
                • Use Cases
            • Key & Certificate File Formats
          • Key Distribution
          • Key Storage
          • Key Rotation
          • Key Revocation
        • Encryption & Decryption
          • Symmetric Encryption
            • Algorithm
            • Modes of Operation
            • Examples
          • Asymmetric Encryption
            • Algorithm
            • Mode of Operation
            • Examples
    • Utilities & Libraries
      • Apache Libraries
        • Apache Camel
          • Camel Architecture
            • Camel Context
            • Camel Endpoints
            • Camel Components
            • Camel Exchange & MEP
          • Spring Dependency
          • Different Components
            • Camel SFTP
        • Apache Commons Lang
      • MapStruct Mapper
      • Utilities by Spring framework
        • FileCopyUtils
    • General Concepts
      • Spring Boot Artifact Packaging
      • Classpath and Resource Loading
      • Configuration - Mapping Properties to Java Class
      • Validations in Spring Framework
        • Jakarta Validation
          • Jakarta Bean Validation Annotations
    • Practical Guidelines
      • Spring Configuration
      • Spring Code Design
  • Software Testing
    • Software Testing Methodologies
      • Functional Testing
      • Non Functional Testing
    • Software Testing Life Cycle (STLC)
    • Integration Test
      • Dynamic Property Registration
    • Java Test Framework
      • JUnit
        • JUnit 4
          • Examples
        • JUnit 5
          • Examples
        • JUnit 4 vs JUnit 5
  • System Design
    • Low-Level Design (LLD)
      • Programming Paradigms
      • Design Pattern
        • Creational Pattern
        • Structural Pattern
        • Behavioral Pattern
        • Examples
          • Data Collector
          • Payment Processor
      • Object-Oriented Design
        • SOLID Principles
        • GRASP Principles
        • Composition
        • Aggregation
        • Association
      • Design Enhancements
        • Fluent API Design
          • Examples
    • High-Level Design (HLD)
      • CAP Theorem
      • Load Balancer
        • Load Balancer Architecture
        • Load Balancing in Java Microservices
          • Client-Side Load Balancing Example
          • Server-Side Load Balancing Example
        • Load Balancer Monitoring Tool
      • Architecture
        • Event Driven Architecture
          • Java Message Service (JMS)
          • Technologies & Frameworks
            • ActiveMQ
              • Architecture Details
              • Version Overview
              • Naming Convention
              • Message Delivery Guarantee
              • Queues and Topics
      • Scaling
        • Vertical Scaling (Scaling Up)
        • Horizontal Scaling (Scaling Out)
        • Auto-Scaling
        • Database Scaling via Sharding
      • Caching
        • Pod-Level vs Distributed Caching
      • Networking Metrics
        • Types of Delay
        • Scenario
      • System Characteristics
      • Workload Types
      • Resilience & Failure Handling
    • Performance
      • Why Is My API Sometimes Slow ?
    • Security
      • Security by Design
      • Zero Trust Security Model
      • Zero Trust Architecture
      • Principles
        • CIA
        • Least Privilege Principle
        • Defense in Depth
      • Security Threats & Mitigations
        • OWASP
          • Top 10 Security Threats
          • Application Security Verification Standard
          • Software Assurance Maturity Model
          • Dependency Check
          • CSRFGuard
          • Cheat Sheets
          • Security Testing Guide
          • Threat Dragon
        • Threat Modeling
      • Compliance & Regulation
        • PCI DSS
    • Deployment Patterns
    • Diagrams
      • UML Diagrams
        • PlantUML
          • Class Diagram
          • Object Diagram
          • Sequence Diagram
          • Use Case Diagram
          • Activity Diagram
          • State Diagram
          • Architecture Diagram
          • Component Diagram
          • Timing Diagram
          • ER Diagram (Entity-Relationship)
          • Network Diagram
    • Common Terminologies
    • Problems
      • Reference Materials
      • Cache Design
  • Interview Guide
    • Non-Technical
      • Behavioural or Introductory Guide
      • Project Specific
    • Technical
      • Java Interview Companion
        • Java Key Concepts
          • Set 1
          • Set 2
        • Java Code Snippets
        • Java Practice Programs
          • Set 3 - Strings
          • Set 4 - Search
          • Set 5 - Streams and Collection
      • SQL Interview Companion
        • SQL Practice Problems
          • Set 1
      • Spring Interview Companion
        • Spring Key Concepts
          • Set 1 - General
          • Set 2 - Core Spring
        • Spring Code Snippets
          • JPA
      • Application Server
      • Maven
      • Containerized Application
      • Microservices
    • General
      • Applicant Tracking System (ATS)
      • Flowchart - How to Solve Coding Problem?
Powered by GitBook
On this page
  • String Manipulation Functions
  • REPLACE
  • TRANSLATE
  • LENGTH
  • TRIM
  • LOWER
  • UPPER
  • RPAD
  • LPAD
  • SUBSTR
  • INSTR
  • REGEXP_LIKE
  • REGEXP_REPLACE
  • Datatype Conversion
  • CAST
  • Hierarchical Function
  • SYS_CONNECT_BY_PATH
  • Null Handling Functions
  • NVL
  • COALESCE
  • Arithmetic Functions
  • AVG
  • MIN
  • MAX
  • COUNT
  • SUM
  • EXP
  • LN
  • LOG
  • POWER
  • SQRT
  • ROUND
  • TRUNC
  • MOD
  • CEIL
  • FLOOR
  • ABS
  • MEDIAN
  • PERCENTILE_CONT
  • Window functions
  • LAG
  • LEAD
  • ROW_NUMBER
  • RANK
  • DENSE_RANK
  • NTILE
  • Date Functions
  • ADD_MONTHS
  • TO_CHAR (with Date Formatting)
  • TO_DATE
  • MONTHS_BETWEEN
  • TRUNC
  • LAST_DAY
  • NEXT_DAY
  • EXTRACT
  • Conditional Functions
  • DECODE
  • CASE
  • Advanced Aggregation Function
  • GROUPING Function
  • ROLLUP Function
  • CUBE

Was this helpful?

  1. Database
  2. SQL Databases
  3. SQL Fundamentals
  4. SQL Functions

Oracle Specific

String Manipulation Functions

REPLACE

Description

The REPLACE function in Oracle is used to replace occurrences of a specified substring within a string with another substring. It is a string manipulation function that can be used to modify parts of a string based on specific criteria. It is case-sensitive.

Syntax

REPLACE(string, search_string, replacement_string)

Parameters

  • string: The original string in which the search and replace operation will be performed.

  • search_string: The substring that we want to find within the original string.

  • replacement_string: The substring that will replace every occurrence of the search_string. If replacement_string is omitted, all occurrences of search_string are removed.

Return Value

The function returns a new string where all occurrences of search_string are replaced with replacement_string.

Examples

SELECT REPLACE('Hello World', 'World', 'Oracle') AS replaced_string
FROM DUAL;
-- Output -> Hello Oracle

SELECT REPLACE('Hello World', 'World') AS replaced_string
FROM DUAL;
-- Output -> Hello

SELECT REPLACE('banana', 'a', 'o') AS replaced_string
FROM DUAL;
-- Output -> bonono

SELECT FIRST_NAME, REPLACE(FIRST_NAME, 'a', 'o') AS new_name
FROM employees;

TRANSLATE

Description

The TRANSLATE function in Oracle is used to replace characters in a string with other characters based on a given mapping. Unlike REPLACE, which operates on substrings, TRANSLATE operates on individual characters and can simultaneously replace multiple characters with different characters.

Parameters

  • string: The original string in which the replacement will take place.

  • from_string: A string containing characters to be replaced.

  • to_string: A string containing characters that will replace the characters in from_string.

Return Value

The function returns a new string where each character in the from_string is replaced with the corresponding character in the to_string.

  • The from_string and to_string should have the same length. If to_string is shorter, extra characters in from_string are removed from the string.

  • Each character in from_string is mapped to the character in the same position in to_string.

  • If any character in from_string does not have a corresponding character in to_string, that character is removed from the string.

  • Character-by-Character Replacement i.e. each character in from_string is replaced by the character at the same position in to_string.

  • Works for character-by-character translation and requires non-empty mapping strings.

Examples

SELECT TRANSLATE('banana', 'a', 'o') AS translated_string
FROM DUAL;
-- Output -> bonono

SELECT TRANSLATE('banana', 'an', 'om') AS translated_string
FROM DUAL;
-- Output -> bomomo

SELECT TRANSLATE('banana', 'a', '') AS translated_string
FROM DUAL;
-- Output -> null

SELECT FIRST_NAME, TRANSLATE(FIRST_NAME, 'ae', 'oi') AS new_name
FROM employees;

How TRANSLATE differs from REPLACE?

Feature
REPLACE
TRANSLATE

Purpose

Replace occurrences of a substring

Replace occurrences of individual characters

Syntax

REPLACE(string, search_string, replacement_string)

TRANSLATE(string, from_string, to_string)

Operation

Works on substrings

Works on individual characters

Case Sensitivity

Case-sensitive

Case-sensitive

Character Mapping

One-to-one mapping for the entire substring

One-to-one character mapping

Use Case Example

Replace all occurrences of "cat" with "dog"

Replace all occurrences of 'a' with 'b', 'e' with 'i'

When search_string or from_string is not found

No change

No change

Handling of NULLs

If any argument is NULL, returns NULL

If any argument is NULL, returns NULL

Performance

Generally used for fewer, more complex replacements

Generally faster for character replacements

Removing Characters

Cannot directly remove a substring without replacing

Can remove characters by providing an empty to_string. If we want to remove multiple characters using TRANSLATE, we need to ensure that the to_string parameter is not empty but contains a character that will be mapped to the characters in the from_string

Example 1

REPLACE('Hello World', 'World', 'Oracle') returns Hello Oracle

TRANSLATE('Hello', 'Hlo', 'Bri') returns Berro

Example 2

REPLACE('banana', 'a', 'o') returns bonono

TRANSLATE('banana', 'an', 'om') returns bomomo

Use in Columns

SELECT REPLACE(column_name, 'old', 'new') FROM table

SELECT TRANSLATE(column_name, 'from_chars', 'to_chars') FROM table

LENGTH

Description

The LENGTH function in Oracle is used to return the number of characters in a string.

Syntax

LENGTH(string)

Parameters

  • string: The string expression whose length you want to determine. This can be a column name, a literal string, or any expression that evaluates to a string.

Return Value

The function returns an integer representing the number of characters in the specified string. If the string is NULL, the function returns NULL.

Examples

SELECT LENGTH('Hello World') AS length_of_string
FROM DUAL;
-- Output -> 11

SELECT FIRST_NAME, LENGTH(FIRST_NAME) AS length_of_name
FROM employees;

TRIM

Description

The TRIM function in Oracle is used to remove leading and trailing spaces or other specified characters from a string. It is particularly useful for cleaning up data by removing unnecessary whitespace or specific unwanted characters from the beginning and end of strings.

Syntax

TRIM([ [LEADING | TRAILING | BOTH] [trim_character] FROM ] string)

Parameters

  • LEADING | TRAILING | BOTH: Specifies which side(s) of the string to trim. The default is BOTH.

    • LEADING: Trims characters from the beginning of the string.

    • TRAILING: Trims characters from the end of the string.

    • BOTH: Trims characters from both the beginning and the end of the string.

  • trim_character: The character to be trimmed from the string. If not specified, spaces are trimmed by default.

  • string: The string expression from which the characters are to be trimmed.

Return Value

The function returns a new string with the specified characters removed from the specified side(s).

Examples

-- Remove leading and trailing spaces from a string
SELECT TRIM('  Hello World  ') AS trimmed_string FROM DUAL;
-- Output -> Hello World

-- Remove leading and trailing asterisks (*) from a string
SELECT TRIM('*' FROM '***Hello***') AS trimmed_string FROM DUAL;
-- Output -> Hello

-- Remove only the leading spaces from a string
SELECT TRIM(LEADING ' ' FROM '  Hello World  ') AS trimmed_string FROM DUAL;
-- Output -> Hello World 

-- Remove only the trailing spaces from a string
SELECT TRIM(TRAILING ' ' FROM '  Hello World  ') AS trimmed_string FROM DUAL;
-- Output ->

-- Using TRIM with a Column
SELECT FIRST_NAME, TRIM(FIRST_NAME) AS trimmed_name
FROM employees;

LOWER

Description

The LOWER function converts all characters in a string to lowercase.

Syntax

LOWER(string)

Parameters

  • string: The input string to be converted to lowercase.

Return Value

The function returns a new string with all characters converted to lowercase.

Examples

SELECT LOWER('Hello World') AS lower_string FROM DUAL;
-- Output -> hello world

SELECT LOWER(FIRST_NAME) AS lower_name FROM employees;

UPPER

Description

The UPPER function converts all characters in a string to uppercase.

Syntax

UPPER(string)

Parameters

  • string: The input string to be converted to uppercase.

Return Value

The function returns a new string with all characters converted to uppercase.

Examples

SELECT UPPER('Hello World') AS upper_string FROM DUAL;
-- Output -> HELLO WORLD

SELECT UPPER(FIRST_NAME) AS upper_name FROM employees;

RPAD

Description

The RPAD function pads the right side of a string with a specified character to a specified length.

Syntax

RPAD(string, length, [pad_string])

Parameters

  • string: The input string to be padded.

  • length: The total length of the resulting string after padding.

  • pad_string (optional): The string to pad with. If not specified, spaces are used.

Return Value

The function returns a new string of the specified length, padded with the specified character (or spaces) on the right side.

Examples

-- Pad a string with spaces to a length of 10
SELECT RPAD('Hello', 10) AS padded_string FROM DUAL;
-- Output -> Hello     

-- Pad a string with asterisks (*) to a length of 10
SELECT RPAD('Hello', 10, '*') AS padded_string FROM DUAL;
-- Output -> Hello*****

SELECT RPAD(FIRST_NAME, 15, '-') AS padded_name FROM employees;

LPAD

Description

The LPAD function pads the left side of a string with a specified character to a specified length.

Syntax

LPAD(string, length, [pad_string])

Parameters

  • string: The input string to be padded.

  • length: The total length of the resulting string after padding.

  • pad_string (optional): The string to pad with. If not specified, spaces are used.

Return Value

The function returns a new string of the specified length, padded with the specified character (or spaces) on the left side.

Examples

-- Pad a string with spaces to a length of 10
SELECT LPAD('Hello', 10) AS padded_string FROM DUAL;
-- Output ->     Hello

-- Pad a string with asterisks (*) to a length of 10
SELECT LPAD('Hello', 10, '*') AS padded_string
FROM DUAL;
-- Output -> *****Hello

-- Pad a column
SELECT LPAD(FIRST_NAME, 15, '-') AS padded_name
FROM employees;

SUBSTR

Description

The SUBSTR function in Oracle extracts a substring from a string, starting from a specified position and optionally for a specified length.

Syntax

SUBSTR(string, start_position, [length])

Parameters

  • string: The input string from which the substring will be extracted.

  • start_position: The position at which the extraction starts. This is a 1-based index.

    • If start_position is positive, it starts from the beginning of the string.

    • If start_position is negative, it starts from the end of the string and counts backwards.

  • length (optional): The number of characters to extract. If omitted, the function returns the substring from start_position to the end of the string.

Positive Start Position: Starts extracting from the beginning of the string.

Negative Start Position: Starts extracting from the end of the string.

Length: If not specified, extraction goes to the end of the string.

Return Value: Returns NULL if start_position is beyond the string length or if length is less than or equal to 0

Return Value

The function returns a substring of the specified length starting from the specified position. If start_position is beyond the length of the string, the function returns NULL. If length is less than or equal to 0, the function returns NULL.

Examples

-- Extract a substring from the 7th character to the end of the string
SELECT SUBSTR('Hello World', 7) AS substring FROM DUAL;
-- Output -> World

-- Extract 5 characters starting from the 1st character
SELECT SUBSTR('Hello World', 1, 5) AS substring FROM DUAL;
-- Output -> Hello

-- Extract 5 characters starting from the 6th character from the end
SELECT SUBSTR('Hello World', -6, 5) AS substring FROM DUAL;
-- Output -> World

-- Extracting from a Column
SELECT FIRST_NAME, SUBSTR(FIRST_NAME, 1, 3) AS first_three_chars
FROM employees;

INSTR

Description

The INSTR function in Oracle is used to search for a substring within a string and return the position at which the substring is found. If the substring is not found, it returns 0. This function is useful for string manipulation and searching within text data.

Syntax

INSTR(string, substring [, start_position [, nth_appearance]])

Parameters

  • string: The string to be searched.

  • substring: The substring to search for.

  • start_position (optional): The position in the string to start the search. The default is 1.

  • nth_appearance (optional): Specifies which occurrence of the substring to search for. The default is 1.

Return Value

The function returns the position of the first character of the nth occurrence of the substring in the string. If the substring is not found, it returns 0.

Examples

SELECT INSTR('Oracle Database', 'a') AS position
FROM DUAL;
-- Output -> 2

SELECT INSTR('Oracle Database', 'a', 3) AS position
FROM DUAL;
-- Output -> 8

SELECT INSTR('Oracle Database', 'a', 1, 2) AS position
FROM DUAL;
-- Output -> 12

SELECT INSTR('Oracle Database', 'x') AS position
FROM DUAL;
-- Output -> 0

REGEXP_LIKE

Description

The REGEXP_LIKE function in Oracle is used to perform regular expression matching. It is a powerful function for pattern matching and is particularly useful for complex search conditions. It allows to search for a string that matches a regular expression pattern.

Syntax

REGEXP_LIKE(source_string, pattern [, match_parameter])

Parameters

  • source_string: The string to be searched.

  • pattern: The regular expression pattern to search for.

  • match_parameter (optional): A string that can include one or more of the following modifiers:

    • 'i': Case-insensitive matching.

    • 'c': Case-sensitive matching (default).

    • 'n': The period . does not match the newline character.

    • 'm': The string is treated as multiple lines. The ^ and $ match the start and end of any line within the source string.

Common regular expression symbols

Symbol
Description

^

Matches the start of a string.

$

Matches the end of a string.

.

Matches any single character except newline.

[]

Matches any single character within the brackets.

[^]

Matches any single character not within the brackets.

*

Matches 0 or more occurrences of the preceding element.

+

Matches 1 or more occurrences of the preceding element.

?

Matches 0 or 1 occurrence of the preceding element.

{n}

Matches exactly n occurrences of the preceding element.

{n,}

Matches n or more occurrences of the preceding element.

{n,m}

Matches at least n and at most m occurrences of the preceding element.

\d

Matches any digit (equivalent to [0-9]).

\D

Matches any non-digit.

\w

Matches any word character (alphanumeric plus underscore).

\W

Matches any non-word character.

\s

Matches any whitespace character (space, tab, newline).

\S

Matches any non-whitespace character.

\b

Matches a word boundary.

\B

Matches a non-word boundary.

()

Groups expressions and captures the matched text.

(?:...)

Groups expressions without capturing the matched text.

(?=...)

Positive lookahead; matches if the expression within the parentheses can be matched.

(?!...)

Negative lookahead; matches if the expression within the parentheses cannot be matched.

(?<=...)

Positive lookbehind; matches if the preceding text matches the expression within the parentheses.

(?<!...)

Negative lookbehind; matches if the preceding text does not match the expression within the parentheses.

\

Escapes a special character to match it literally.

Examples

-- Return employees whose first name starts with 'J'
SELECT first_name FROM employees WHERE REGEXP_LIKE(first_name, '^J');

-- Case-Insensitive Matching
SELECT first_name FROM employees
WHERE REGEXP_LIKE(first_name, '^j', 'i');

-- Return employees whose first name is either 'John' or 'Jane'
SELECT first_name FROM employees
WHERE REGEXP_LIKE(first_name, '^(John|Jane)$');

-- Returns employees whose first name contains any digit
SELECT first_name FROM employees
WHERE REGEXP_LIKE(first_name, '[0-9]');

-- Returns employees whose first name starts with 'A' and is exactly 4 characters long
SELECT first_name FROM employees
WHERE REGEXP_LIKE(first_name, '^A.{3}$');

-- Returns employees whose first name ends with 'smith', regardless of case
SELECT first_name FROM employees
WHERE REGEXP_LIKE(first_name, 'smith$', 'i');

-- Returns comments that contain lines starting with 'Thank you', treating the comment as multiple lines
SELECT comment FROM feedback
WHERE REGEXP_LIKE(comment, '^Thank you', 'm')

-- Check if a string is exactly 4 characters long
SELECT 'Match' AS result
FROM DUAL
WHERE REGEXP_LIKE('Test', '^.{4}$');

-- Check if a string contains 'abc' and is not affected by newlines
SELECT 'Match' AS result
FROM DUAL
WHERE REGEXP_LIKE('abc\ndef', 'abc', 'n');

-- Check if any line in a multiline string starts with 'Hello'
SELECT 'Match' AS result
FROM DUAL
WHERE REGEXP_LIKE('First line\nHello world', '^Hello', 'm');

-- Check if a string does not contain any digits
SELECT 'Match' AS result
FROM DUAL
WHERE REGEXP_LIKE('HelloWorld', '^\D*$');

REGEXP_REPLACE

The REGEXP_REPLACE function in Oracle is used to search a string for a regular expression pattern and replace it with another string. It is useful for advanced string manipulation where patterns and replacements can be specified with regular expressions.

Syntax

REGEXP_REPLACE(source_string, pattern, replace_string [, position [, occurrence [, match_parameter]]])

Parameters

  • source_string: The string to search within.

  • pattern: The regular expression pattern to search for.

  • replace_string: The string to replace the matched pattern with.

  • position (optional): The position in the source string to start the search. The default is 1.

  • occurrence (optional): The occurrence of the pattern to be replaced. The default is 0, meaning all occurrences.

  • match_parameter (optional): A string that can include one or more of the following modifiers:

    • 'i': Case-insensitive matching.

    • 'c': Case-sensitive matching (default).

    • 'n': The period . does not match the newline character.

    • 'm': The string is treated as multiple lines. The ^ and $ match the start and end of any line within the source string.

Examples

-- Replace all occurrences of a pattern
SELECT REGEXP_REPLACE('123abc456', '[0-9]', 'X') AS result
FROM DUAL;
-- Output -> XXXabcXXX

-- Replace First Occurrence
SELECT REGEXP_REPLACE('123abc456', '[0-9]', 'X', 1, 1) AS result
FROM DUAL;
-- Output -> X23abc456

-- Replace all occurrences of a pattern, ignoring case
SELECT REGEXP_REPLACE('abcABCabc', 'a', 'X', 1, 0, 'i') AS result
FROM DUAL;
-- Output -> XbcXBCXbc

-- Use captured groups in the replacement string
SELECT REGEXP_REPLACE('abc123', '([a-z]+)([0-9]+)', '\2\1') AS result
FROM DUAL;
-- Output -> 123abc

-- Replace all non-word characters with a space
SELECT REGEXP_REPLACE('Hello, World! 123.', '\W', ' ') AS result
FROM DUAL;
-- Output -> Hello World 123

-- Remove all digits from a string
SELECT REGEXP_REPLACE('Phone: 123-456-7890', '\d', '') AS result
FROM DUAL;
-- Output -> Phone: --

-- Replace the first character of each line in a multiline string
SELECT REGEXP_REPLACE('Line1\nLine2\nLine3', '^.', 'X', 1, 0, 'm') AS result
FROM DUAL;
-- Output -> Xine1\nXine2\nXine3

Datatype Conversion

CAST

Description

The CAST function in Oracle is used to convert one data type to another. It is a versatile function that allows to change the data type of an expression to a different data type, which is particularly useful when dealing with different types of data in queries.

Syntax

CAST(expression AS target_data_type)

Parameters

  • expression: The value or column that you want to convert.

  • target_data_type: The data type to which you want to convert the expression.

Supported Data Types

The CAST function supports conversion to and from a wide range of data types like

  • VARCHAR2, CHAR

  • NUMBER, INTEGER, FLOAT

  • DATE, TIMESTAMP

  • BLOB, CLOB

Examples

-- Convert a VARCHAR2 column to a NUMBER:
SELECT CAST('123' AS NUMBER) AS number_value FROM DUAL;

-- Convert a NUMBER to a VARCHAR2
SELECT CAST(123 AS VARCHAR2(10)) AS string_value FROM DUAL;

-- Convert a DATE to a TIMESTAMP
SELECT CAST(SYSDATE AS TIMESTAMP) AS timestamp_value FROM DUAL;

-- Using CAST in a Table Query
SELECT employee_id, hire_date, CAST(hire_date AS TIMESTAMP) AS hire_timestamp FROM employees;

-- Combining CAST with Other Functions
SELECT CAST(ROUND(123.456, 2) AS VARCHAR2(10)) AS rounded_string FROM DUAL;

-- Oracle will raise an error
SELECT CAST('abc' AS NUMBER) AS invalid_conversion FROM DUAL;

NULL Handling: If the expression is NULL, the CAST function returns NULL of the target data type.

Hierarchical Function

SYS_CONNECT_BY_PATH

Description

The SYS_CONNECT_BY_PATH function in Oracle is used in hierarchical queries to return the path of a column value from the root to the current row in a tree-structured format. It is particularly useful for displaying hierarchical relationships in a readable format.

Syntax

SYS_CONNECT_BY_PATH(column, delimiter)

Parameters

  • column: The column for which the path is to be constructed.

  • delimiter: The character or string that separates the levels in the hierarchy.

Return Value

The function returns a string that represents the path from the root node to the current node, with each level separated by the specified delimiter.

Usage Context

The SYS_CONNECT_BY_PATH function is used in the context of hierarchical queries, which are queries that use the CONNECT BY clause to define parent-child relationships within the data.

If the column used in SYS_CONNECT_BY_PATH is NULL, it includes the NULL values in the path.

Examples

  1. Employee Manager Hierarchy

We have an employees table with columns EMPLOYEE_ID, MANAGER_ID, and FIRST_NAME. We need to display the hierarchical path of employee names with the help of SYS_CONNECT_BY_PATH

SELECT EMPLOYEE_ID, FIRST_NAME, 
       SYS_CONNECT_BY_PATH(FIRST_NAME, ' -> ') AS hierarchy_path
FROM employees
START WITH MANAGER_ID IS NULL
CONNECT BY PRIOR EMPLOYEE_ID = MANAGER_ID;

Sample Output

  1. Create sequence or generate data

SELECT LEVEL, SYS_CONNECT_BY_PATH(LEVEL, ' -> ') AS level_path
FROM DUAL
CONNECT BY LEVEL <= 5;

Sample Output

Null Handling Functions

NVL

Description

The NVL function in Oracle is used to replace NULL values with a specified value. This function is particularly useful when dealing with potential NULL values in your data and you want to ensure that your results are complete and meaningful.

Syntax

NVL(expression1, expression2)

Parameters

  • expression1: The expression to be checked for NULL.

  • expression2: The value to be returned if expression1 is NULL.

Return Value

The function returns expression2 if expression1 is NULL. If expression1 is not NULL, it returns expression1.

Examples

SELECT NVL(NULL, 'Default Value') AS result FROM DUAL;
-- Output -> Default Value

SELECT FIRST_NAME, NVL(FIRST_NAME, 'Unknown') AS name
FROM employees;

COALESCE

Description

The COALESCE function in Oracle is used to return the first non-null expression among its arguments. It's a more versatile and flexible alternative to the NVL function, as it can handle multiple expressions and return the first one that is not NULL.

Syntax

COALESCE(expr1, expr2, ..., exprn)

Parameters

  • expr1, expr2, ..., exprn: The expressions to be evaluated in the order they are provided. The function returns the first non-null expression.

COALESCE stops evaluating expressions once it finds the first non-null expression, potentially improving performance.

Return Value

The function returns the first non-null expression among its arguments. If all arguments are NULL, it returns NULL.

Examples

-- Return the first non-null value
SELECT COALESCE(NULL, NULL, 'First Non-Null', 'Second Non-Null') AS result
FROM DUAL;
-- Output -> First Non-Null

-- Employees table with columns commission_pct, bonus, and salary
SELECT employee_id, 
       COALESCE(commission_pct, bonus, salary) AS compensation
FROM employees;

Arithmetic Functions

AVG

The AVG function calculates the average value of a numeric column.

Syntax

SELECT AVG(column_name) AS avg_value
FROM table_name;

MIN

The MIN function returns the smallest value in a set.

Syntax

SELECT MIN(column_name) AS min_value
FROM table_name;

MAX

The MAX function returns the largest value in a set.

Syntax

SELECT MAX(column_name) AS max_value
FROM table_name;

COUNT

The COUNT function returns the number of rows that match a specified condition.

Syntax

SELECT COUNT(column_name) AS count_value
FROM table_name;

SUM

The SUM function returns the total sum of a numeric column.

Syntax

SELECT SUM(column_name) AS sum_value
FROM table_name;

When we do COUNT(*), counting is rows (regardless of actual value, which is why rows containing NULL and non-NULL values are counted). But when we do COUNT a column, we are counting the number of non-NULL values in that column.

EXP

The EXP function returns e raised to the power of a given number. The constant e is approximately equal to 2.71828.

Syntax

SELECT EXP(2) AS exp_value
FROM DUAL;
-- Output -> 7.389056

LN

The LN function returns the natural logarithm (base e) of a given number.

Syntax

SELECT LN(7.389056) AS ln_value
FROM DUAL;
-- Output -> 2

LOG

The LOG function returns the logarithm of a given number with a specified base.

Syntax

SELECT LOG(base, n) AS log_value
FROM DUAL;

POWER

The POWER function returns a number raised to the power of another number.

Syntax

SELECT POWER(m, n) AS power_value
FROM DUAL;

SQRT

The SQRT function returns the square root of a given number.

Syntax

SELECT SQRT(n) AS sqrt_value
FROM DUAL;

ROUND

The ROUND function returns a number rounded to a specified number of decimal places.

Syntax

SELECT ROUND(n, decimals) AS rounded_value
FROM DUAL;

Example

Round 123.4567 to two decimal places:

SELECT ROUND(123.4567, 2) AS rounded_value
FROM DUAL;
-- Output -> 123.46

TRUNC

The TRUNC function truncates a number to a specified number of decimal places, effectively removing the fractional part beyond the specified decimal places.

Syntax

SELECT TRUNC(n, decimals) AS truncated_value
FROM DUAL;

Example

Truncate 123.4567 to two decimal places:

SELECT TRUNC(123.4567, 2) AS truncated_value
FROM DUAL;
-- Output -> 123.45

MOD

The MOD function returns the remainder of a division operation.

Syntax

SELECT MOD(m, n) AS mod_value
FROM DUAL;

Example

Calculate the remainder of 10 divided by 3:

SELECT MOD(10, 3) AS mod_value
FROM DUAL;
-- Output -> 1

CEIL

The CEIL function returns the smallest integer greater than or equal to a given number.

Syntax

SELECT CEIL(n) AS ceil_value
FROM DUAL;

Example

Calculate the ceiling of 123.456:

SELECT CEIL(123.456) AS ceil_value
FROM DUAL;
-- Output -> 124

FLOOR

The FLOOR function returns the largest integer less than or equal to a given number.

Syntax

SELECT FLOOR(n) AS floor_value
FROM DUAL;

Example

Calculate the floor of 123.456:

SELECT FLOOR(123.456) AS floor_value
FROM DUAL;
-- Output -> 123

ABS

The ABS function returns the absolute value of a number.

Syntax

SELECT ABS(n) AS abs_value
FROM DUAL;

Example

Calculate the absolute value of -123.456:

SELECT ABS(-123.456) AS abs_value
FROM DUAL;
-- Output -> 123.456

MEDIAN

The MEDIAN function computes the median of a set of values. The median is the value separating the higher half from the lower half of a data sample.

Syntax

MEDIAN(value_expression) OVER ( [partition_by_clause] order_by_clause )

Example

Assume we have a table employees with a salary column. To calculate the median salary for the entire table:

-- calculate the median salary for the entire table
SELECT 
    MEDIAN(salary) OVER () AS median_salary
FROM 
    employees;
    
-- calculate the median salary for each department
SELECT 
    department_id, 
    MEDIAN(salary) OVER (PARTITION BY department_id) AS median_salary
FROM 
    employees;    

PERCENTILE_CONT

The PERCENTILE_CONT function is an inverse distribution function that returns the value corresponding to the specified percentile in a group of values.

Syntax

PERCENTILE_CONT(percentile) WITHIN GROUP (ORDER BY value_expression) 
OVER ( [partition_by_clause] )
  • percentile: A numeric value between 0 and 1. For example, 0.5 represents the 50th percentile (median).

  • value_expression: The column or expression on which the percentile calculation is performed.

Example

Calculate the median (50th percentile) salary for the entire table:

SELECT 
    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) AS median_salary
FROM 
    employees;

Calculate the 90th percentile salary for each department:

SELECT 
    department_id, 
    PERCENTILE_CONT(0.9) WITHIN GROUP (ORDER BY salary) 
    OVER (PARTITION BY department_id) AS perc90_salary
FROM 
    employees;

Window functions

Window functions in SQL, also known as analytic functions, allows to perform calculations across a set of table rows that are somehow related to the current row. This is similar to aggregate functions but unlike aggregate functions, window functions do not cause rows to become grouped into a single output row—the rows retain their separate identities.

LAG

The LAG function provides access to a row at a given physical offset prior to the current row within the partition.

Syntax

LAG(value_expression [, offset] [, default]) OVER ( [partition_by_clause] order_by_clause )
  • value_expression: The column or expression to evaluate.

  • offset: The number of rows back from the current row from which to obtain the value (default is 1).

  • default: The value to return if the offset goes out of the bounds of the partition.

Example

--  Get the hire date of the previous employee for each row in the employees table
SELECT 
    employee_id, 
    hire_date, 
    LAG(hire_date, 1) OVER (ORDER BY hire_date) AS prev_hire_date
FROM 
    employees;

LEAD

The LEAD function provides access to a row at a given physical offset following the current row within the partition.

Syntax

LEAD(value_expression [, offset] [, default]) OVER ( [partition_by_clause] order_by_clause )
  • value_expression: The column or expression to evaluate.

  • offset: The number of rows back from the current row from which to obtain the value (default is 1).

  • default: The value to return if the offset goes out of the bounds of the partition.

Example

-- Get the hire date of the previous employee for each row in the employees table
SELECT 
    employee_id, 
    hire_date, 
    LEAD(hire_date, 1) OVER (ORDER BY hire_date) AS next_hire_date
FROM 
    employees;

ROW_NUMBER

The ROW_NUMBER function assigns a unique number to each row to which it is applied, starting from 1.

Syntax

ROW_NUMBER() OVER ( [partition_by_clause] order_by_clause )

Example

-- Assign a unique rank to each employee based on their salary in descending order
SELECT 
    employee_id, 
    salary, 
    ROW_NUMBER() OVER (ORDER BY salary DESC) AS rank
FROM 
    employees;

RANK

The RANK function provides the rank of a row within the partition of a result set. The rank of a row is one plus the number of ranks that come before it.

Syntax

RANK() OVER ( [partition_by_clause] order_by_clause )

Example

-- Ranks employees based on their salary, in descending order. 
-- Rows with equal values receive the same rank, and the next rank value will be skipped.
SELECT 
    employee_id, 
    salary, 
    RANK() OVER (ORDER BY salary DESC) AS salary_rank
FROM 
    employees;

DENSE_RANK

The DENSE_RANK function is similar to RANK, but it does not skip rank values if there are ties.

Syntax

DENSE_RANK() OVER ( [partition_by_clause] order_by_clause )

Example

-- Ranks employees based on their salary, in descending order, without skipping any rank values.
SELECT 
    employee_id, 
    salary, 
    DENSE_RANK() OVER (ORDER BY salary DESC) AS salary_dense_rank
FROM 
    employees;

NTILE

The NTILE function distributes the rows in an ordered partition into a specified number of groups, and assigns a number to each row indicating the group to which it belongs.

Syntax

NTILE(num_buckets) OVER ( [partition_by_clause] order_by_clause )

Example

-- Divides the employees into four groups based on their salary, 
-- and assigns each row a number indicating its quartile.
SELECT 
    employee_id, 
    salary, 
    NTILE(4) OVER (ORDER BY salary DESC) AS salary_quartile
FROM 
    employees;

Date Functions

ADD_MONTHS

The ADD_MONTHS function adds a specified number of months to a date.

Syntax

ADD_MONTHS(date, number_of_months)
  • date: The starting date.

  • number_of_months: The number of months to add (can be positive or negative).

Example

-- Add 3 months to the current dat
SELECT ADD_MONTHS(SYSDATE, 3) AS new_date
FROM DUAL;

-- Subtract 2 months from a specific date
SELECT ADD_MONTHS(TO_DATE('2023-05-25', 'YYYY-MM-DD'), -2) AS new_date
FROM DUAL;

TO_CHAR (with Date Formatting)

The TO_CHAR function converts a date or number to a string using a specified format.

Syntax

TO_CHAR(date, 'format_model')
  • date: The date value to be converted.

  • format_model: The format in which the date should be returned. Common format models include:

    • 'YYYY': Four-digit year.

    • 'MM': Two-digit month.

    • 'DD': Two-digit day.

    • 'DY': Three-character day of the week (e.g., MON, TUE).

    • 'HH24': Hour of the day in 24-hour format.

    • 'MI': Minutes.

    • 'SS': Seconds.

Example

SELECT 
    TO_CHAR(SYSDATE, 'YYYY-MM-DD') AS formatted_date,
    TO_CHAR(SYSDATE, 'DY') AS day_of_week,
    TO_CHAR(SYSDATE, 'HH24:MI:SS') AS time_of_day
FROM DUAL;

TO_DATE

The TO_DATE function converts a string to a date using a specified format.

Syntax

TO_DATE(string, 'format_model')
  • string: The string to be converted to a date.

  • format_model: The format in which the string is provided. The format model should match the string format.

Example

SELECT 
    TO_DATE('2024-05-25', 'YYYY-MM-DD') AS converted_date
FROM DUAL;

SELECT 
    TO_DATE('25-MAY-2024 14:30:00', 'DD-MON-YYYY HH24:MI:SS') AS converted_date
FROM DUAL;

MONTHS_BETWEEN

The MONTHS_BETWEEN function returns the number of months between two dates. The result can include a fractional part if the dates are not exactly a whole number of months apart.

Syntax

MONTHS_BETWEEN(date1, date2)
  • date1: The later date.

  • date2: The earlier date.

Example

SELECT 
    MONTHS_BETWEEN(TO_DATE('2024-05-25', 'YYYY-MM-DD'), TO_DATE('2024-01-15', 'YYYY-MM-DD')) AS months_diff
FROM 
    DUAL;
-- Output -> 4.3225806

TRUNC

The TRUNC function truncates a date to the specified unit of measure. This function is often used to remove the time portion of a date or to truncate the date to a specific level (e.g., year, month).

Syntax

TRUNC(date [, format])
  • date: The date to be truncated.

  • format: The unit of measure to truncate the date to (optional). Common format models include:

    • 'YYYY': Truncate to the first day of the year.

    • 'MM': Truncate to the first day of the month.

    • 'DD': Truncate to midnight of the current day (default if format is not specified).

Example

SELECT 
    TRUNC(TO_DATE('2024-05-25 14:30:00', 'YYYY-MM-DD HH24:MI:SS')) AS trunc_day,
    TRUNC(TO_DATE('2024-05-25 14:30:00', 'YYYY-MM-DD HH24:MI:SS'), 'MM') AS trunc_month,
    TRUNC(TO_DATE('2024-05-25 14:30:00', 'YYYY-MM-DD HH24:MI:SS'), 'YYYY') AS trunc_year
FROM 
    DUAL;

LAST_DAY

The LAST_DAY function returns the last day of the month that contains the specified date.

Syntax

LAST_DAY(date)
  • date: The date from which to determine the last day of the month.

Example

-- Get the last day of the month for a specific date
SELECT 
    LAST_DAY(TO_DATE('2024-05-15', 'YYYY-MM-DD')) AS last_day_of_month
FROM 
    DUAL;
-- Output --> 2024-05-31

NEXT_DAY

The NEXT_DAY function returns the date of the first specified weekday that is later than the given date.

Syntax

NEXT_DAY(date, 'day_of_week')
  • date: The starting date.

  • day_of_week: The name of the weekday (in English). It can be abbreviated (e.g., 'MON' for Monday).

Example

-- Find the next Monday after a specific date
SELECT 
    NEXT_DAY(TO_DATE('2024-05-15', 'YYYY-MM-DD'), 'MONDAY') AS next_monday
FROM 
    DUAL;
-- Output --> 2024-05-20

EXTRACT

The EXTRACT function extracts a specific part of a date (such as year, month, day, hour, minute, second) or an interval.

Syntax

EXTRACT(part FROM date)
  • part: The part of the date to extract. Common parts include YEAR, MONTH, DAY, HOUR, MINUTE, SECOND.

  • date: The date from which to extract the part.

Example

-- Extract the year, month, and day from a date
SELECT 
    EXTRACT(YEAR FROM TO_DATE('2024-05-15', 'YYYY-MM-DD')) AS year,
    EXTRACT(MONTH FROM TO_DATE('2024-05-15', 'YYYY-MM-DD')) AS month,
    EXTRACT(DAY FROM TO_DATE('2024-05-15', 'YYYY-MM-DD')) AS day
FROM 
    DUAL;
-- Output
--YEAR	MONTH	DAY
--2024	5	15

Conditional Functions

DECODE

The DECODE function provides functionality similar to a CASE statement. It allows to perform conditional querying and can transform data within a query based on specific conditions.

Syntax

DECODE(expression, search1, result1, search2, result2, ..., default)
  • expression: The value to be evaluated.

  • search1, search2, ...: The values to compare against the expression.

  • result1, result2, ...: The results to return if the corresponding search value matches the expression.

  • default: The default result to return if no match is found (optional).

Example

Suppose we have an employees table with an employee_id and a department_id, and want to translate department IDs into department names.

SELECT
    employee_id,
    department_id,
    DECODE(department_id,
           10, 'Finance',
           20, 'HR',
           30, 'IT',
           40, 'Sales',
           'Unknown') AS department_name
FROM
    employees;

CASE

The CASE statement in Oracle SQL is a versatile conditional expression that allows to implement conditional logic directly in your SQL queries. It is similar to the DECODE function but more powerful and flexible because it can handle complex conditions and multiple data types.

Syntax

There are two types of CASE statements: the simple CASE statement and the searched CASE statement.

Simple CASE Statement

The simple CASE statement compares an expression to a set of simple expressions to determine the result.

CASE expression
    WHEN value1 THEN result1
    WHEN value2 THEN result2
    ...
    ELSE default_result
END

Searched CASE Statement

The searched CASE statement evaluates a set of Boolean expressions to determine the result.

CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    ...
    ELSE default_result
END

Examples

Example 1: Simple CASE Statement

Suppose we have an employees table with columns employee_id, first_name, last_name, and department_id, and want to translate department IDs into department names.

SELECT
    employee_id,
    first_name,
    last_name,
    department_id,
    CASE department_id
        WHEN 10 THEN 'Finance'
        WHEN 20 THEN 'HR'
        WHEN 30 THEN 'IT'
        WHEN 40 THEN 'Sales'
        ELSE 'Unknown'
    END AS department_name
FROM
    employees;

Example 2: Searched CASE Statement

Suppose we want to classify employees based on their salary ranges.

SELECT
    employee_id,
    first_name,
    last_name,
    salary,
    CASE
        WHEN salary < 30000 THEN 'Low'
        WHEN salary BETWEEN 30000 AND 70000 THEN 'Medium'
        WHEN salary > 70000 THEN 'High'
        ELSE 'Unknown'
    END AS salary_level
FROM
    employees;

Example 3: Combining CASE with Other SQL Features

You can combine the CASE statement with other SQL clauses such as ORDER BY and FETCH FIRST.

SELECT
    employee_id,
    first_name,
    last_name,
    salary,
    department_id,
    CASE department_id
        WHEN 10 THEN 'Finance'
        WHEN 20 THEN 'HR'
        WHEN 30 THEN 'IT'
        WHEN 40 THEN 'Sales'
        ELSE 'Unknown'
    END AS department_name,
    CASE
        WHEN salary < 30000 THEN 'Low'
        WHEN salary BETWEEN 30000 AND 70000 THEN 'Medium'
        WHEN salary > 70000 THEN 'High'
        ELSE 'Unknown'
    END AS salary_level
FROM
    employees
ORDER BY
    salary DESC
FETCH FIRST 5 ROWS ONLY;

Advanced Aggregation Function

GROUPING Function

The GROUPING function is used to distinguish between a detail row and an aggregate row created by a ROLLUP or CUBE operation. It returns 1 for a row created by ROLLUP or CUBE and 0 for a regular row.

Syntax

GROUPING(column_name)

Example 1

SELECT
    product_id,
    region_id,
    SUM(sales_amount) AS total_sales,
    GROUPING(product_id) AS is_product_total,
    GROUPING(region_id) AS is_region_total
FROM
    (
        SELECT 1 AS product_id, 1 AS region_id, 100 AS sales_amount FROM dual UNION ALL
        SELECT 1 AS product_id, 2 AS region_id, 150 AS sales_amount FROM dual UNION ALL
        SELECT 2 AS product_id, 1 AS region_id, 200 AS sales_amount FROM dual UNION ALL
        SELECT 2 AS product_id, 2 AS region_id, 250 AS sales_amount FROM dual
    )
GROUP BY ROLLUP (product_id, region_id);
  • is_product_total is 1 when the row is a product subtotal.

  • is_region_total is 1 when the row is a region subtotal.

  • Both are 1 when the row is the grand total.

Example 2

SELECT
    product_id,
    SUM(sales_amount) AS total_sales,
    GROUPING(product_id) AS is_product_total
FROM
    (
        SELECT 1 AS product_id, 1 AS region_id, 100 AS sales_amount FROM dual UNION ALL
        SELECT 1 AS product_id, 2 AS region_id, 150 AS sales_amount FROM dual UNION ALL
        SELECT 2 AS product_id, 1 AS region_id, 200 AS sales_amount FROM dual UNION ALL
        SELECT 2 AS product_id, 2 AS region_id, 250 AS sales_amount FROM dual
    )
GROUP BY ROLLUP (product_id);

ROLLUP Function

The ROLLUP function is an extension to the GROUP BY clause that creates subtotals and a grand total. It allows you to create aggregate values at multiple levels of a hierarchy.

Syntax

SELECT
    columns,
    aggregate_function(column_name)
FROM
    table
GROUP BY ROLLUP (columns);

ROLLUP vs CUBE

Feature
CUBE
ROLLUP

Purpose

Generates subtotals for all possible combinations of a set of dimensions.

Generates subtotals and grand totals for a hierarchical set of dimensions.

Hierarchical Levels

Covers all combinations of the specified columns, resulting in a full cross-tabulation of subtotals.

Covers a hierarchy of the specified columns, resulting in subtotal rows at each level of the hierarchy and a grand total.

Number of Groupings

2^n (where n is the number of columns). For example, for 3 columns, it generates 8 groupings.

n+1 (where n is the number of columns). For example, for 3 columns, it generates 4 groupings.

Subtotals

Includes subtotals for all possible combinations, including combinations of 1, 2, up to n-1 columns.

Includes subtotals for each column and progressively fewer columns until only the grand total is left.

Grand Total

Included as one of the combinations where all specified columns are set to NULL.

Included as the final grouping where all specified columns are set to NULL.

Use Case

Useful for comprehensive data analysis with all possible dimension combinations.

Useful for hierarchical data analysis where subtotals are needed at each level of the hierarchy.

Example Syntax

GROUP BY CUBE (col1, col2, col3)

GROUP BY ROLLUP (col1, col2, col3)

Example 1

SELECT
    product_id,
    region_id,
    SUM(sales_amount) AS total_sales
FROM
    (
        SELECT 1 AS product_id, 1 AS region_id, 100 AS sales_amount FROM dual UNION ALL
        SELECT 1 AS product_id, 2 AS region_id, 150 AS sales_amount FROM dual UNION ALL
        SELECT 2 AS product_id, 1 AS region_id, 200 AS sales_amount FROM dual UNION ALL
        SELECT 2 AS product_id, 2 AS region_id, 250 AS sales_amount FROM dual
    )
GROUP BY ROLLUP (product_id, region_id);
  • Rows where region_id is NULL are subtotals for each product_id.

  • The row where both product_id and region_id are NULL is the grand total.

Example 2

SELECT
    product_id,
    SUM(sales_amount) AS total_sales
FROM
    (
        SELECT 1 AS product_id, 1 AS region_id, 100 AS sales_amount FROM dual UNION ALL
        SELECT 1 AS product_id, 2 AS region_id, 150 AS sales_amount FROM dual UNION ALL
        SELECT 2 AS product_id, 1 AS region_id, 200 AS sales_amount FROM dual UNION ALL
        SELECT 2 AS product_id, 2 AS region_id, 250 AS sales_amount FROM dual
    )
GROUP BY ROLLUP (product_id);

CUBE

The CUBE function in Oracle SQL is an extension of the GROUP BY clause that generates subtotals for all possible combinations of a given set of dimensions. It is useful for producing a cross-tabulation of data and can create a comprehensive result set that includes subtotals and a grand total for multi-dimensional data analysis.

Syntax

SELECT
    column1,
    column2,
    aggregate_function(column3)
FROM
    table
GROUP BY CUBE (column1, column2);

Example 1

SELECT
    product_id,
    region_id,
    SUM(sales_amount) AS total_sales
FROM
    (
        SELECT 1 AS product_id, 1 AS region_id, 100 AS sales_amount FROM dual UNION ALL
        SELECT 1 AS product_id, 2 AS region_id, 150 AS sales_amount FROM dual UNION ALL
        SELECT 2 AS product_id, 1 AS region_id, 200 AS sales_amount FROM dual UNION ALL
        SELECT 2 AS product_id, 2 AS region_id, 250 AS sales_amount FROM dual
    )
GROUP BY CUBE (product_id, region_id);
  • Rows where region_id is NULL are subtotals for each product_id.

  • Rows where product_id is NULL are subtotals for each region_id.

  • The row where both product_id and region_id are NULL is the grand total.

Example 2

SELECT
    product_id,
    SUM(sales_amount) AS total_sales
FROM
    (
        SELECT 1 AS product_id, 1 AS region_id, 100 AS sales_amount FROM dual UNION ALL
        SELECT 1 AS product_id, 2 AS region_id, 150 AS sales_amount FROM dual UNION ALL
        SELECT 2 AS product_id, 1 AS region_id, 200 AS sales_amount FROM dual UNION ALL
        SELECT 2 AS product_id, 2 AS region_id, 250 AS sales_amount FROM dual
    )
GROUP BY CUBE (product_id);
PreviousSQL FunctionsNextSQL Data Types

Last updated 8 months ago

Was this helpful?