Date_trunc error to truck the user activity on website - sql

I'm using google cloud and trying to format the event_timestamp column to extract the date and time to identifying time that user take to make a purchase on website
DATE_TRUNC(DATE (event_timestamp), 'month') AS purchase_date,
as per the query, I got an error "A valid date part name is required at [6:31]"
the dateset
event_timestamp
1605430896492843
expecting results example
Purchase_date
2020-11-15 12:27:20

From the docs it seems, that it should be without quotes:
DATE_TRUNC(DATE (event_timestamp), MONTH) AS purchase_date

Change the order:
DATE_TRUNC('month', DATE (event_timestamp)) AS purchase_date

Since your event_timestamp value consists of 16 digits, you need to use the timestamp_micros function.
timestamp_micros Interprets int64_expression as the number of microseconds since 1970-01-01 00:00:00 UTC and returns a timestamp.
select timestamp_micros(event_timestamp) AS purchase_date
This will output (2020-11-15 09:01:36.492843 UTC) for the input (1605430896492843).
The date_trunc /timestamp_trunc function will truncate the date to the given date_part, so according to your expected output you don't need to use it.
select timestamp_trunc(timestamp_micros(1605430896492843), month) AS purchase_date
This will output (2020-11-01 00:00:00 UTC)

Related

When to use DATE_TRUNC() vs. DATE_PART()?

I do not know when to use DATE_TRUNC and DATE_PART() in a query.
I have not really tried much, just some web searches that I do not fully grasp but I just started learning SQL (Postgres).
They both do very different things. One truncates a date to the precision specified (kind of like rounding, in a way) and the other just returns a particular part of a datetime.
From the documentation:
date_part():
The date_part function is modeled on the traditional Ingres equivalent
to the SQL-standard function extract:
date_part('field', source)
Note that here the field parameter needs to be a string value, not a
name. The valid field names for date_part are the same as for extract.
For historical reasons, the date_part function returns values of type
double precision. This can result in a loss of precision in certain
uses. Using extract is recommended instead.
SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');
Result: 16
SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
Result: 4
date_trunct():
The function date_trunc is conceptually similar to the trunc function
for numbers.
date_trunc(field, source [, time_zone ]) source is a value expression
of type timestamp, timestamp with time zone, or interval. (Values of
type date and time are cast automatically to timestamp or interval,
respectively.) field selects to which precision to truncate the input
value. The return value is likewise of type timestamp, timestamp with
time zone, or interval, and it has all fields that are less
significant than the selected one set to zero (or one, for day and
month).
...
Examples (assuming the local time zone is America/New_York):
SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-02-16 20:00:00
SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-01-01 00:00:00
SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00');
Result: 2001-02-16 00:00:00-05
SELECT date_trunc('day', TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40+00', 'Australia/Sydney');
Result: 2001-02-16 08:00:00-05
SELECT date_trunc('hour', INTERVAL '3 days 02:47:33');
Result: 3 days 02:00:00

Group timestamp information by date and hour in Bigquery

I have this timestamp metric which shows the following information:
2021-08-30 22:10:22.838 UTC
I would like to split and group this info by date and hour, so it should look something like this in BQ:
Date: 2021-08-30
Hour: 22:00:00 UTC
Anyone know how do do this?
Thanks!!
To extract the date and hour you can use this (replacing your_ts with the appropriate field name).
SELECT
EXTRACT(DATE FROM your_ts) dt,
EXTRACT(HOUR FROM your_ts) hr
FROM tbl
If you want to keep the formatting you provided (returning strings), you can try something like this.
SELECT
FORMAT_TIMESTAMP("%F", your_ts) dt,
FORMAT_TIMESTAMP("%X", TIMESTAMP_TRUNC(your_ts, HOUR))
FROM tbl

Postgres: How do I extract year and month from a date?

One of my columns is a date type in the following format: YYYY-MM-DD. I want to extract YYYY-MM. So far, the resources I've come across show me that I can extract either year using SELECT extract(year from order_date)... but I can't figure out how to extract both the year and the month. I tried the following but it didn't work: https://www.w3schools.com/sql/func_mysql_extract.asp
I just want to point out that it is often convenient to leave the value as a date. If so, use date_trunc():
select date_trunc('month', order_date) as yyyymm
If you really want a string, you should accept Nick's answer.
In PostgreSQL you can use TO_CHAR():
SELECT TO_CHAR(order_date, 'YYYY-MM')
Output (if order_date = '2020-04-06'):
2020-04
Note if your column is not already a date or timestamp you will need to cast it to a date or timestamp (e.g. order_date::date).
Demo on dbfiddle

How to get the actual timestamp minus 2 months and the first day without time part in h2 database?

In H2 I would like to get the actual timestamp minus 2 months and on the first day of month without the time part?
eg.: 2020-03-09 13:46:55 => 2020-01-01 00:00:00
Thanks a lot
Try this:
select FORMATDATETIME(DATEADD(mm,-2,CURRENT_DATE) ,'Y-MM-01');
SELECT DATE_TRUNC('MONTH', TIMESTAMP '2020-03-09 13:46:55' - INTERVAL '2' MONTH)
/
SELECT DATE_TRUNC('MONTH', LOCALTIMESTAMP - INTERVAL '2' MONTH)
should be used in recent releases of H2. It isn't supported by historic versions, however.
FORMATDATETIME is slow, has different known bugs, and it produces a VARCHAR value that needs an additional implicit or explicit cast back to a datetime value.

Date_Trunc not function working as expected

I am trying to use the Date_Trunc for MONTH function in a SQL statement but somehow it is not working for me. I am trying to pull entries which happen after April 1st, 2019. The raw date format from the Redshift database is this format which I am trying to group into month/year buckets: 2019-04-08T00:13:20.000Z
Input
SELECT
client_id as user_id,
session_utc as job_date --(format:2019-04-08T00:13:20.000Z)
FROM table1 as hits
WHERE job_date >= DATE_TRUNC('month', 2019-04-01)
group by 1,2;
Output
"ERROR: function date_trunc("unknown", integer) does not exist Hint: No function matches the given name and argument types. You may need to add explicit type casts."
What am I doing wrong?
The DATE_TRUNC Function - Amazon Redshift takes timestamp as input and provides a timestamp as output:
DATE_TRUNC('datepart', timestamp)
For example:
SELECT DATE_TRUNC('month', '2019-05-07'::timestamp)
2019-05-01 00:00:00
Therefore, your line should read:
WHERE job_date >= DATE_TRUNC('month', '2019-04-01'::timestamp)
If you wish to have the output as a date, append ::date:
SELECT DATE_TRUNC('month', '2019-05-07'::timestamp)::date
2019-05-01
Also, note that the date converts into a timestamp as at midnight. This can cause a difference for some comparisons. For example:
'2019-05-07 03:03:31.389324+00'::timestamp > '2019-05-07'::timestamp
will evaluate as True because it is comparing to midnight at the start of the day. This is different to comparing two dates (without timestamps).
The syntax for the function is DATE_TRUNC('datepart', timestamp), seems you need to use as DATE_TRUNC('month', session_utc)(this already truncates to the first date of April 2019 i.e. 2019-04-01 )
Assuming you are using Postgres, you need quotes around your date constant and can convert to the right types:
WHERE job_date >= DATE_TRUNC('month'::text, '2019-04-01'::date)
Here is a db<>fiddle.