General Functions with SQL

General Functions with SQL

General functions are built-in operations provided by SQL databases to manipulate and analyze data. These functions cover various tasks, including string manipulation, numeric calculations, and date handling.

String Functions
UPPER: Converts a string to uppercase. 

 
Result: ‘HELLO WORLD’
LOWER: Converts a string to lowercase. 

 Result: ‘hello world’
SUBSTR or SUBSTRING: Extracts a substring from a string. 

 Result: ‘hello’
CONCAT: Concatenates two or more strings. 

Result: ‘hello world’
TRIM: Removes leading and trailing spaces from a string.

Result: ‘hello world’

Numeric Functions
ROUND: Rounds a number to a specified number of decimal places. 

 Result: 123.46
FLOOR: Rounds a number down to the nearest integer. 

Result: 123
CEIL or CEILING: Rounds a number up to the nearest integer. 

 Result: 124
ABS: Returns the absolute value of a number. 

 Result: 123.45

 

 Date Functions
SYSDATE or CURRENT_DATE: Returns the current date

Result: The current date.
ADD_MONTHS: Adds a specified number of months to a date. 

 Result: The date one month from today.
EXTRACT: Extracts a part of a date (e.g., year, month). 

Result: The current year.
DATEDIFF: Returns the difference between two dates (in SQL Server). 

Result: The number of days between January 1, 2024, and today.

 

Conditional Expressions in SQL
Conditional expressions allow you to execute different logic based on conditions within your SQL queries. The most common conditional expressions are CASE and DECODE.

CASE Expression
Purpose: Performs conditional logic within a SELECT statement, allowing you to return different values based on specific conditions.
Syntax

 

Examples:
Simple CASE: 

Description: Assigns a department name based on the department_id.
Searched CASE: 

Description: Categorizes salaries into ranges based on their values.

DECODE Function (Oracle SQL Specific)
Purpose: A simpler alternative to CASE for straightforward conditional logic.
Syntax

 Examples:
Simple DECODE:

Description: Returns different results based on the department_id.

 

Applying General Functions and Conditional Expressions in SELECT
You can combine general functions with conditional expressions to perform complex data manipulations and analyses in your SELECT statements.
Example 1: Formatting Dates and Conditional Logic 

Description: Formats the hire_date to display the year and month, and categorizes employees based on their years of experience.
Example 2: Conditional Formatting with String and Numeric Functions 

Description: Formats the full name, adjusts the salary by 10%, and categorizes the salary status based on its value.

Summary
General functions and conditional expressions are essential tools for manipulating and analyzing data in SQL. They allow you to format data, perform calculations, and apply logic to generate meaningful results. By combining these functions and expressions in SELECT statements, you can create complex queries that meet various analytical needs.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print