1

I am looking to convert dates from my date column in my table from the Gregorian format '12-02-2023' to Hijri (Islamic Lunar Calendar) format '01-03-1445' format.

I would like to later get results only for a particular Hijri month, lets say 9.

I have tried searching but did not get any answers that are relevant in Google Bigquery context.

Standard SQL has the 130/131 format to get Hijri dates but unsure how the application is for Bigquery.

select convert(datetime, value, 131)

1 Answer 1

2

You can achieve this by using User defined functions (UDF) in BigQuery. With UDF you can write custom Javascript code and import custom libraries to achieve what you are looking for.

CREATE OR REPLACE FUNCTION test.gregorianToHijri(gregorian_date DATE) RETURNS STRING LANGUAGE js 
  OPTIONS (
    library=['gs://<bucket-name>/moment.js', 'gs://<bucket-name>/moment-hijri.js'])
AS """

  const hijriDate = moment(gregorian_date).format('iYYYY-iM-iD');
  return hijriDate;
""";

Test query:

  SELECT test.gregorianToHijri(DATE '2023-02-12') AS hijri_date;

Output:

hijri_date -> 1444-7-20

Please replace the with your own bucket name before testing the above code. If the libraries do not meet your requirements, you can customize the code within the UDF in the way you want and just call the function using SQL. You can check more about UDF and how to import Javascript libraries into your UDF through the document

6
  • Great answer @fahed. I deployed your function in public datasets so that in can be available to anyone without install using: select bigfunctions.eu.gregorian2hijri(date '2023-02-12') Here is the doc to use it: unytics.io/bigfunctions/bigfunctions/#gregorian2hijri Commented Feb 29 at 10:34
  • @PaulMarcombes Can you help demonstrate an example on how I can use the function to my use? I have the column dates where the dates are stored in Gregorian. I want my resulting table to show dates for my data in Hijri and extract using a particular Hijri month lets say.
    – m996
    Commented Feb 29 at 12:30
  • @m996 You can run the above select query shared and give the input as shown above. If this helps you, you can accept my answer. Commented Feb 29 at 12:33
  • @FahedSabellioglu Where do I get the bucket-name from? I have tried the project ID and it does not work as well. I am getting the error. "Not found: Dataset projectid:test was not found in location US. My dataset is stored in EU. Sorry this is a bit new to me. Can you help providing detailed instructions on how to deploy and use it in my example to convert the dates and get the result as per the resulting month. Appreciate the help.
    – m996
    Commented Feb 29 at 12:41
  • @m996 It depends on the location of your dataset. Assuming your_dataset in in US multi-region you can do: select bigfunctions.us.gregorian2hijri(date_column) from your_dataset.your_table . If your_dataset is in asia-south1 region, you can do: select bigfunctions.asia_south1.gregorian2hijri(date_column) from your_dataset.your_table . Etc Commented Feb 29 at 14:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.