Dealing with Different Date Formats When Using STRPTIME() in DuckDB

In DuckDB, the strptime() function converts a date/time string into a valid timestamp value. We pass a format string to the function in order to tell it what format our string uses. This can be handy if we ever need to construct timestamps based on date/time strings that may or may not be in a valid format.

But what if we have multiple date/time strings in different formats?

Fortunately, the strptime() function caters for this scenario too.

Continue reading

5 Ways to Get the Data Type of Columns Output by a Query in DuckDB

Usually when we run a SQL query it’s important to know the data type of the columns returned by that query. If we’re familiar with the data, then we might instinctively know the data type of each column. But even then, we might only have a general idea, such as “it’s a date” or “it’s a number” without knowing the exact details.

Fortunately, DuckDB provides us with several ways to find out the data type of the columns returned by a query.

Continue reading

LIST_REVERSE() vs LIST_REVERSE_SORT() in DuckDB: What’s the Difference?

DuckDB has a good range of functions for dealing with lists and arrays. Amongst these are list_reverse() and a list_reverse_sort(). Looking at their names, you could be forgiven for thinking that these do the same thing. But they don’t.

If you’re wondering why DuckDB has a list_reverse() and a list_reverse_sort() function, read on.

Continue reading

Convert Column Values to a JSON Array with DuckDB’s JSON_GROUP_ARRAY() Function

The json_group_array() function in DuckDB is used to aggregate values into a JSON array, making it especially useful when working with structured or semi-structured data such as JSON. This function is part of DuckDB’s JSON extension and works similarly to DuckDB’s string_agg() or the group_concat() function in some other RDBMSs, but instead of returning a delimited string, it returns a well-formed JSON array.

This function is particularly helpful when we need to represent grouped or hierarchical data in a JSON format for export, reporting, or further transformation.

Continue reading

Subtract Hours from a Date/Time Value in DuckDB

When working with SQL databases, one operation we often find ourselves performing is date/time arithmetic, such as adding or subtracting an interval to/from a date/time value. Fortunately, most RDBMSs make such operations quite easy to achieve, and DuckDB is no exception.

Below are two methods we can use in order to subtract hours from a date, timestamp, or time value in DuckDB.

Continue reading

Get the Abbreviated Month Name in DuckDB

When working with dates in DuckDB, sometimes we need to extract date parts from date or timestamp values. And when it comes to date parts like days and months, we have the option of getting the numeric representation or the actual name. And if we want the name, we have a further option of getting the full name or the shortened version.

For example, we can get December or we can get Dec.

Continue reading

3 Ways to Get the Weighted Average in DuckDB

Weighted averages are common calculations in data analysis, allowing us to assign different levels of importance to individual values in our dataset. Unlike simple averages, where each value has equal impact, weighted averages let us incorporate the relative significance of each observation. This is particularly valuable for scenarios like calculating GPA (where courses have different credit weights), investment portfolio returns (where assets have varying allocations), or quality ratings (where reviewers have different expertise levels).

In this article, we’ll explore three ways of calculating weighted averages in DuckDB.

Continue reading

A Quick Look at LIMIT & OFFSET in DuckDB

Most database management systems (DBMSs) provide us with a means of restricting the number of rows returned by a query to a fixed number of rows, or to a percentage of the data set. In many cases this is done with a LIMIT clause (although some DBMSs provide other methods, such as SQL Server’s TOP clause).

When it comes to DuckDB, the LIMIT clause is what’s implemented for this functionality.

Continue reading