- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Databases
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
SQL - DATE_BUCKET() Function
The SQL DATE_BUCKET() function enables you to group data into groups that correspond to fixed periods of time. According to the arguments supplied to the function, it returns the datetime value that marks the beginning of each datetime bucket.
The Date Bucket function, as its name suggests, computes date buckets with a single defined size. The function will return the start date of the bucket holding the date in the event that it is supplied with a date and the bucket size.
Syntax
Following is the syntax of the SQL DATE_BUCKET() function −
DATE_BUCKET (datepart, number, date, origin)
Parameters
This function accepts four parameters. The same is discussed below −
datepart − part of the date that is used with number. Ex. Year, minutes, hours etc.
number − it is an integer that going to decide the width of the bucket combined with datepart argument. It should be a positive integer value as it represents the width of the datepart buckets from the origin time.
date − DATE_BUCKET accepts an expression, column expressions or user defined variables for date, if they resolved to any of these data types, such as date, datetime, datetime2, datetimeoffset, smalldatetime, time
origin − the origin datatype should match the datatype of the date parameter. DATE_BUCKET uses the default origin date value, if no origin value is specified for function.
Example
Let us look into the following example on SQL DATE_BUCKET() function where the width is 1 day wide by using following query −
DECLARE @date date = '2023-02-21', @origin date = '2023-02-05'; SELECT DATE_BUCKET(DAY, 1, @date, @origin);
Output
When we execute the above query, the output is obtained as follows −
+------------+ | Result | +------------+ | 2023-02-21 | +------------+
Example
Here is another scenario, where we are going to increment the argument to 2,3,4 respectively by using the following query −
DECLARE @date date = '2023-02-21', @origin date = '2023-02-05'; SELECT DATE_BUCKET(day, 1, @date, @origin) AS "1 day", DATE_BUCKET(day, 2, @date, @origin) AS "2 days", DATE_BUCKET(day, 3, @date, @origin) AS "3 days", DATE_BUCKET(day, 4, @date, @origin) AS "4 days";
Output
When the query gets executed, it will generate the output as shown below −
+------------+------------+------------+------------+ | 1day | 2 days | 3 days | 4 days | +------------+------------+------------+------------+ | 2023-02-21 | 2023-02-21 | 2023-02-20 | 2023-02-21 | +------------+------------+------------+------------+
Example
In the following example, we are going to set a 7 days wide, but we there are only 5 days left between @date and @origin let us look the result by using the following query −
DECLARE @date date = '2023-02-21', @origin date = '2023-02-17'; SELECT DATE_BUCKET(day, 7, @date, @origin) AS OriginDate;
Output
On executing the above query, the output is displayed as follows−
+------------+ | OriginDate | +------------+ | 2023-02-17 | +------------+
Example
Let us look into the another scenario, where we are using the 7 days wide, and increasing the gap between the two date and checking the result by using the following query −
DECLARE @date date = '2023-02-21', @origin date = '2023-02-10'; SELECT DATE_BUCKET(day, 7, @date, @origin) AS Result;
Output
When the query, gets executed it will generate the output as shown below −
+------------+ | Result | +------------+ | 2023-02-17 | +------------+
Example
Let us look into another example, where we are going to use user defined variables as arguments for number, date and checking the result by running the following query −
DECLARE @days int = 365, @datetime datetime2 = '2023-02-21 18:03:59.8684429';; SELECT Date_Bucket(DAY, @days, @datetime) AS Result;
Output
The output for the above query is produced as given below −
+-----------------------------+ | Result | +-----------------------------+ | 2022-12-02 00:00:00.0000000 | +-----------------------------+
Example
In the following example we are using the SYSDATETIME() for date by using the following query −
SELECT Date_Bucket(WEEK, 08, SYSDATETIME()) AS Result;
Output
When the query gets executed, it will generate an output as shown below −
+-----------------------------+ | Result | +-----------------------------+ | 2023-02-13 00:00:00.0000000 | +-----------------------------+
Example
Here is another scenario, where we are using SYSDATETIME() and numeric expression(07/2) as arguments for number and date by using the following query −
SELECT Date_Bucket(WEEK,(07/2), SYSDATETIME()) AS Result;
Output
On running the above query, it will generate an output as shown below −
+-----------------------------+ | Result | +-----------------------------+ | 2023-02-06 00:00:00.0000000 | +-----------------------------+
Example
Consider the following example, where we are using the non-default origin value for generating the date_bucket by using the following query −
Declare @date datetime2 = '2023-02-21 11:34:22'; declare @origin datetime2 = '2023-01-01 00:00:00'; Select DATE_BUCKET(HOUR, 3, @date, @origin) AS Result;
Output
On running the above query, it will generate an output as shown below −
+-----------------------------+ | Result | +-----------------------------+ | 2023-02-21 09:00:00.0000000 | +-----------------------------+
To Continue Learning Please Login
Login with Google