Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract month name on a string datatype on athena

SELECT sales_invoice_date,
       MONTH( DATE_TRUNC('month',
                         CASE
                           WHEN TRIM(sales_invoice_date) = '' THEN
                           DATE('1999-12-31')
                          ELSE
                           DATE_PARSE(sales_invoice_date, '%m/%d/%Y')
                         END) ) AS DT
  FROM testdata_parquet

I used the query above to convert the string into date and was able to get the month number on AWS athena but I wasn't able to get the corresponding month name

I have already tried monthname and datename('month', ...) but they gave the following error messages respectively:

SYNTAX_ERROR: line 2:1: Function monthname not registered

SYNTAX_ERROR: line 2:1: Function datename not registered

like image 309
Ray Avatar asked Dec 26 '19 08:12

Ray


People also ask

How do I cast a data type in Athena?

To convert data in arrays to supported data types, use the CAST operator, as CAST(value AS type) . Athena supports all of the native Presto data types. To use the Amazon Web Services Documentation, Javascript must be enabled.

What kind of data type does Amazon Athena support?

Q: What data formats does Amazon Athena support? Amazon Athena supports a wide variety of data formats like CSV, TSV, JSON, or Textfiles and also supports open source columnar formats such as Apache ORC and Apache Parquet. Athena also supports compressed data in Snappy, Zlib, LZO, and GZIP formats.

What is struct data type in Athena?

When you use CREATE_TABLE , Athena defines a STRUCT in it, populates it with data, and creates the ROW data type for you, for each row in the dataset. The underlying ROW data type consists of named fields of any supported SQL data types.


2 Answers

Athena is currently based on Presto .172, so you should refer to https://trino.io/docs/0.172/functions/datetime.html for available functions on date/time values.

You can get month name with date_format():

date_format(value, '%M')

or similarly format_datetime().

format_datetime(value, 'MMM')

Example:

presto:default> SELECT date_format(current_date, '%M');
  _col0
----------
 December
(1 row)

(verified on Presto 327, but will work in Athena too)

like image 138
Piotr Findeisen Avatar answered Oct 19 '22 19:10

Piotr Findeisen


You can use to_char() function with 'month' argument :

to_char(sales_invoice_date, 'month')

in order to return the month names.

like image 1
Barbaros Özhan Avatar answered Oct 19 '22 19:10

Barbaros Özhan