Date and system functions are built-in tools in SQL and programming languages to manage time and retrieve server information. They allow you to automate timestamps, calculate durations, and identify current database users. 📅 Current Date and Time Functions These functions capture the exact moment a query runs.
GETDATE() / NOW(): Returns the current system date and time.
CURRENT_DATE / CURDATE(): Extracts only the current calendar date. CURRENT_TIME: Extracts only the current clock time.
SYSDATE(): Returns the exact time the function executes (unlike NOW(), which returns the query start time). 🧮 Date Manipulation and Calculations
Use these functions to modify dates or find the gap between two events.
DATEADD(unit, value, date): Adds or subtracts time (days, months, years) from a date.
DATEDIFF(unit, start, end): Calculates the exact time difference between two dates.
DATE_FORMAT() / TO_CHAR(): Converts a raw date into a readable string format (e.g., “MM-DD-YYYY”).
EXTRACT(unit FROM date): Pulls a specific part (like just the year or month) out of a date. 🖥️ Essential System Functions
These functions look inward to provide metadata about the database environment.
USER / CURRENT_USER: Identifies the account name running the query.
DATABASE() / CURRENT_DATABASE: Displays the name of the active database.
VERSION() / @@VERSION: Reveals the specific software version of the database engine.
HOST_NAME(): Identifies the workstation terminal connected to the server. 🛠️ Data Cleanup and Safety Functions
These system functions handle missing data and errors gracefully.
COALESCE(val1, val2, …): Returns the very first non-null value in a list.
ISNULL(val, replacement): Swaps out a missing value (NULL) with a default choice.
CAST() / CONVERT(): Changes a data piece from one type to another (e.g., text to integer). To tailor this breakdown to your needs, please let me know:
Leave a Reply