Ask Your Question
1

How can I transform a standard date to a Jalali date in ClickHouse?

asked 2021-04-29 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-07-14 15:00:00 +0000

nofretete gravatar image

ClickHouse does not have built-in support for converting standard dates to Jalali dates. However, you can write a custom User-Defined Function (UDF) in ClickHouse to implement this conversion.

Here is an example UDF in ClickHouse that converts a standard date (YYYY-MM-DD) to a Jalali date:

CREATE FUNCTION to_jalali_date(date String) -> String
    return strftime('%Y-%m-%d', toDateTime(concat('20', substring(date, 7, 2), '-', substring(date, 4, 2), '-', substring(date, 1, 2))))
    format Str;

In this UDF, we first extract the year, month, and day components of the input date string using the substring() function. We then concatenate them to form a DateTime string in the format expected by ClickHouse: YYYY-MM-DD HH:MM:SS. Finally, we pass this DateTime string to the strftime() function along with the format string %Y-%m-%d to convert the date to Jalali format.

You can use this UDF in your queries like this:

SELECT to_jalali_date('1399/10/22') as jalali_date

-- Output: 1399-10-22

Note that this UDF assumes that the input date string is in the format DD-MM-YYYY. If your input date format is different, you may need to modify the UDF accordingly.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-04-29 11:00:00 +0000

Seen: 8 times

Last updated: Jul 14 '22