![]() |
In SQLite database management the DATE() function is useful for handling date and time data very effectively. This function fasts the extraction of the date part from a datetime expression or conversion of a text representation of a date in date format. In this article, we will learn about the DATE() Function to understand along with its syntax, various examples, and so on. Understanding the SQLite DATE () FunctionAt its core, the DATE() function in SQLite serves to extract the date part from a datetime expression or convert a text representation of a date into a valid date format. It accepts a single argument, typically a datetime expression, and returns the corresponding date portion. Syntax: DATE(datetime_expression, modifier)
Explanation: datetime_expression: A valid datetime expression, which could be a column name, a datetime literal, or a function returning a datetime value. modifier: A modifier that specifies the format in which the date should be returned and can be ‘start of month‘, ‘weekday‘, ‘unixepoch‘, ‘utc‘, ‘localtime‘, ‘localtime‘, ‘localtime‘, ‘localtime’ or ‘NNN days’. Examples of Using the DATE() FunctionTo delve deeper into the functionality of the DATE() function, let’s consider a hypothetical scenario involving a sample table named transactions. This table stores transaction records, including timestamps, in the following schema. CREATE TABLE transactions ( Output: ![]() OUTPUT Example 1: Extract Date From Datetime ExpressionWith the help of DATE() function let’s extract the date part from the transaction_date column without get time in the transactions table. SELECT DATE(transaction_date) AS transaction_date FROM transactions;
Output: ![]() OUTPUT Explanation: We get the date from the transaction_date from transactions table. Example 2: Convert Text Representation to DateThe DATE() function is also used to convert a text representation of a date into a correct date format. Suppose we have insert the new transaction record with a date as a text string. INSERT INTO transactions (description, amount, transaction_date) Output: ![]() After inserting above data Explanation: We have successfully convert the Text to Date. Example 3: Using Date Functions with DATE() FunctionCombining the DATE() function with other date functions allows for more intricate operations. For instance, calculating the current date and extracting only the date part: SELECT DATE('now') AS current_date;
Output: ![]() OUTPUT Explanation: We have get the current date of system using DATE() Function. Time String FormatDATE() function is used with datetime expressions which can be represented in various formats. The time string format refers to the specific structure used to represent time-related data within datetime expressions. Common time string formats defined below in the table:
These time string formats enable precise representation and manipulation of time-related data within SQLite queries ConclusionThe DATE() function is useful for handling date and time data in database applications. Whether extracting the date part from datetime expressions, converting text representations to proper date formats or performing more complex date operations the DATE() function fast data manipulation operations effectively. By understanding the syntax, applications and modifiers of the DATE() function you can enhance your database management capabilities. |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |