4 Functions that Get the Week From a Date in DuckDB

Working with dates often requires extracting specific components, such as the week number, for analysis or reporting. In DuckDB, there are multiple functions to retrieve the week from a date, each catering to different needs. This article explores four such functions, including how to calculate the ISO week—a standard defined by ISO-8601 where weeks start on Monday and the first week of the year contains the year’s first Thursday.

Continue reading

Fix “Unrecognized format for strftime/strptime: %” in DuckDB

If you’re getting an error that includes the text “Unrecognized format for strftime/strptime: %” in DuckDB, it appears that you’re including an unescaped percent sign (%) in your format string when using a function like strftime() or strptime().

Whenever we need a percent sign to be included in the formatted output, we must escape it with another percent sign in the format string.

So to fix this issue, try escaping the percent sign with another percent sign.

Continue reading

Understanding the STRPTIME() Function in DuckDB

DuckDB provides us with a good selection of date/time functions. The strptime() function is a useful one for times where you need to convert a date string into a valid timestamp value; its sole purpose is to parse strings into timestamps.

In this article, we’ll look at how the strptime() function works, along with some examples to demonstrate.

Continue reading

How to Format Dates in DuckDB

When working with SQL databases such as DuckDB, dates usually conform to a specific format. In particular, when we create dates in DuckDB, they must conform to the ISO 8601 format (YYYY-MM-DD). It’s the same with time (hh:mm:ss[.zzzzzz][+-TT[:tt]]) and timestamp values (YYYY-MM-DD hh:mm:ss[.zzzzzzzzz][+-TT[:tt]]).

But what if we have a requirement to present these dates or timestamps in a different format?

Fortunately, DuckDB provides us with tools to so. This article explains how to format date and timestamp values according to a specified format.

Continue reading