Date_Trunc not function working as expected - sql

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.

Related

Date_trunc error to truck the user activity on website

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)

Date_Trunc and To_Date Questions SQL

Can we use date_trunc for a date (not date-time) that we are trying to "truncate" (not sure if the term can be applied here) to e.g. the start of the week? So if I have date_trunc(week, 28/10/2020) and I want to get the start of the week that 28th of October lies in (so 26th of October)? I tried this in my SQL command line but I get error messages.
If I am doing: SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); How can I ensure the resulting format is in a date format I specify (rather than the default date format)? For example if I want it in format DD-MM-YYYY?
select to_char(date '2017-06-02', 'MM') < in this example, why do we need "date" for this to work? The general format for to_char should be TO_CHAR (timestamp_expression, 'format'). I don't see in this general format that we need "day".
if I have a WHERE filter like to_char(order_date, '20-10-2020'), and there are indeed some rows with this order date, will these rows still show in my results (after running query) if these rows are in DATE format (so 20 Oct is in date format) as opposed to string (which is what I am filtering by as I am doing to_char). I know there would be no need to use to_char in this case but just asking..
yes, you can use date in text form but you have to cast it to a correct type
these queries will work
select date_trunc('week', '2020-10-28'::date);
select date_trunc('week', '10/28/2020'::date);
-- as well as
select date_trunc('week', '2020-10-28'::datetime);
and return timestamp 2020-10-26 00:00:00.000000
note, next query
select date_trunc('week', '28/10/2020'::date);
will fail with error date/time field value out of range: "28/10/2020";
You can use to_char, it returns text, if you need a date format you have to case it again
select to_char( to_date ('02 Oct 2001', 'DD Mon YYYY'), 'DD-MM-YYYY')::date;
select to_char('02 Oct 2001'::date, 'DD-MM-YYYY')::date;
'2017-06-02' is a text and it can't be automatically converted to timestamp. Actually I don't know a text format which can.
No, you need to explicitly cast into date type to use it as a filter
where order_date = date_stored_as_a_text::date
I am answering the questions in a different order as there are some wrong assumptions:
Question 3
There is a general difference between '2017-06-02' and date '2017-06-02' - the first one is just a varchar, a string, NOT handled as a date by Redshift, the 2nd one tells Redshift to handle the string as date and therefore works.
Question 2
A date data type column has no format - you may an sql client that can display date columns in different formats, however, this is not a functionality of redshift. SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); tells redshift to convert the string '02 Oct 2001' to date.
Question 1
DATE_TRUNC('datepart', timestamp) also supports week as datepart - see Date parts for date or timestamp function (Also shown in the example of AWS). You should also be able to provide a date instead of a timestamp.
Question 4
to_char(order_date, '20-10-2020')is not a filter and you are using it wrong.
AWS TO_CHAR
TO_CHAR converts a timestamp or numeric expression to a character-string data format.
I guess you are rather looking for:
where to_char(order_date, 'YYYY-MM-DD') = '20-10-2020'

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

google bigquery select from a timestamp column between now and n days ago

I have a dataset in bigquery with a TIMESTAMP column "register_date" (sample value "2017-11-19 22:45:05.000 UTC" ).
I need to filter records based on x days or weeks before today criteria.
Example query
select all records which are 2 weeks old.
Currently I have this query (which I feel like a kind of hack) that works and returns the correct results
SELECT * FROM `my-pj.my_dataset.sample_table`
WHERE
(SELECT
CAST(DATE(register_date) AS DATE)) BETWEEN DATE_ADD(CURRENT_DATE(), INTERVAL -150 DAY)
AND CURRENT_DATE()
LIMIT 10
My question is do I have to use all that CASTing stuff on a TIMESTAMP column (which seems like over complicating the otherwise simple query)?
If I remove the CASting part, my query doesn't run and returns error.
Here is my simplified query
SELECT
*
FROM
`my-pj.my_dataset.sample_table`
WHERE
register_date BETWEEN DATE_ADD(CURRENT_DATE(), INTERVAL -150 DAY)
AND CURRENT_DATE()
LIMIT
10
that results into an error
Query Failed
Error: No matching signature for operator BETWEEN for argument types: TIMESTAMP, DATE, DATE. Supported signature: (ANY) BETWEEN (ANY) AND (ANY) at [6:17]
any insight is highly appreciated.
Use timestamp functions:
SELECT t.*
FROM `my-pj.my_dataset.sample_table` t
WHERE register_date BETWEEN TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL -150 DAY) AND CURRENT_TIMESTAMP()
LIMIT 10;
BigQuery has three data types for date/time values: date, datetime, and timestamp. These are not mutually interchangeable. The basic idea is:
Dates have no time component and no timezone.
Datetimes have a time component and no timezone.
Timestamp has both a time component and a timezone. In fact, it represents the value in UTC.
INTERVAL values are defined in gcp documentation
Conversion between the different values is not automatic. Your error message suggests that register_date is really stored as a Timestamp.
One caveat (from personal experience): the definition of day is based on UTC. This is not much of an issue if you are in London. It can be a bigger issue if you are in another time zone and you want the definition of "day" to be based on the local time zone. If that is an issue for you, ask another question.

Filter timestamp by specific month-year

I am new to postgres and would appreciate any advice. I have postgres table with a timestamp column whose values are in the format: 1970-01-01 00:00:00
My objective is to select records from the last three whole months - December 2016, January 2017 and February 2017. How would one write this query with only read access using SELECT?
When I start with:
SELECT to_char("start_time", 'YYYY-MM-DD HH:MM:SS') FROM trips;
Times are converted to AM/PM but I am only interested in extracting and subsetting by month and year
Here you go:
SELECT *
FROM trips
WHERE start_time BETWEEN '2016-01-01 00:00:00'::timestamp AND '2017-02-28 23:59:59'::timestamp;
You can use extract or date_trunc function to extract month in postgresql.
Very similar to question get last three month records from table
For more details about date time functions in postgresql use below link.
https://www.postgresql.org/docs/9.1/static/functions-datetime.html
Here is one method:
select t.*
from t
where start_date >= date_trunc('month',now() - interval '3' month) and
start_date < date_trunc('month', now());