Athena sql date_diff - sql

Running athena sql query
select date_diff('day' ,checkout_date::date, book_date::date) from users.
book_date and checkout_date are all timestamp.Got an error:
Error running query: function date_diff(unknown, date, date) does not exist ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
Checked presto documentation:
https://prestodb.io/docs/current/functions/datetime.html
date_diff(unit, timestamp1, timestamp2) → bigint
Returns timestamp2 - timestamp1 expressed in terms of unit.

Sample of function is given, probably you need to cast columns in timestamp

Related

Find median of dates in PostgreSQL

I was wondering if there is a way to find the 'median' date in PostgreSQL. The goal is to obtain, according to the median, the date that is in the middle of all the dates.
I tried following modified median function:
select
percentile_cont(0.5) within group (order by date)
from cte
By trying to do so I get the following error message:
SQL Error [42883]: ERROR: function percentile_cont(numeric, timestamp without time zone) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Position: 12
As dates are not supported, I was wondering if there is another way to calculate the median value of a date.
Thank you for any inputs!
You can cast the date value to an integer and then use it for getting the median value using the percentile_cont function.
Like so,
SELECT
percentile_cont(0.5) within group (ORDER by cast(extract(epoch from dateCol1) as integer))
FROM table1
The above gives the median date, but in numeric value, to convert it back to a date type, use the to_timestamp function like so ,
select to_timestamp(1638662400)::date
#gives 2021-12-05

No matching signature for function TIMESTAMP_DIFF.... error on BigQuery

I'm trying to get the timestamp difference between two date fields in my table. I know both dates are on TIMESTAMP format, but when I tried using TIMESTAMP_DIFF function I get an error saying "No matching signature for function TIMESTAMP_DIFF for argument types: STRING, STRING, DATE_TIME_PART. Supported signature: TIMESTAMP_DIFF(TIMESTAMP, TIMESTAMP, DATE_TIME_PART) at [27:8]"
I also tried formatting them again in the query(as done on the example for FIRST_VALUE(): https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators#datetime_diff , and then it showed the same error but for FORMAT_TIMESTAMP.
Any ideas what I could do to fix this or to get the time difference between two fields for each row?
use below (BigQuery Standard SQL)
#standardSQL
SELECT TIMESTAMP_DIFF(PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', prev_time), PARSE_TIMESTAMP('%Y-%m-%d %H:%M:%S', event_datetime), MINUTE)
FROM `project.dataset.messages`

DATE_FROM_UNIX_DATE and UNIX_DATE both return errors on Google BigQuery

The query I'm running as a test is:
SELECT
UNIX_DATE(created_utc)
FROM `fh-bigquery.reddit_comments.2017_08`
But I keep getting this error:
Error: No matching signature for function UNIX_DATE for argument types:
INT64. Supported signature: UNIX_DATE(DATE) at [2:3]
I checked the datatype for the created_utc field and it's an integer. Casting and whatnot won't work either.
Would really appreciate any help. Thanks!
You should use TIMESTAMP_SECONDS() instead
#standardSQL
SELECT
TIMESTAMP_SECONDS(created_utc)
FROM `fh-bigquery.reddit_comments.2017_08`
LIMIT 5
Then you can use DATE() if you need date only
DATE(TIMESTAMP_SECONDS(created_utc))
UNIX_DATE() takes a String.
And DATE_FROM_UNIX_DATE() takes an INT64. SQL has a legacy problem of thinking of time ("date") in DAYS and not SECONDS like Unix. Thus:
SELECT DATE_FROM_UNIX_DATE(CAST(created_utc/86400 as INT64))
FROM `fh-bigquery.reddit_comments.2017_08`

SQL: function trunc(timestamp without time zone, "unknown") does not exist

I am trying to transform a date column to week by using the following code:
select trunc(join_date, 'D') as join_wk from my_table
But I got the following errors:
function trunc(timestamp without time zone, "unknown") does not exist
Hint: No function matches the given name and argument types. You may need to add explicit type casts.
where join_date is of the format:
2017-08-24 14:49:59
What did I do wrong in the query? Thanks!
The name of the function is date_trunk, you have to swap the parameters:
SELECT DATE_TRUNC('D', join_date) AS join_wk FROM my_table

YEAR gives error

pgAdmin III always gives an error on year
ERROR: function year(date) does not exist
LINE 1: SELECT YEAR(geboortedatum) as date_part
.............................^^^^.........
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
ERROR: function year(date) does not exist
SQL state: 42883
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Character: 8
I can get the whole date that I need but I need only the year can someone help me it needs to be the youngest year from the table.
As you mentioned pgAdmin, I assume that you are using Postgres, so you need write code like this (using EXTRACT constructure):
SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');
Detailed documentation for the datetime functions