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
Related
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
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
I'm trying to cast a string to timestamp but I'm getting the following error:
Failed to output to file. Query failed: Value cannot be cast to timestamp: 2020-03-23T05:17:44.000Z
I'm using the query below:
select CAST(purchase_date AS timestamp)
from main_table
You can use from_iso8601_timestamp function if timestamp with time zone type is acceptable.
Or, you can use date_parse function.
Try parse_datetime():
select parse_datetime('2020-03-23T05:17:44.000Z', '%Y-%m-%dT%H:%i:%s.%fZ')
I am needing to convert records where the TEXT values look like this using Postgresql:
26-AUG-2015
to:
2015-08-26
I'm not sure what version of Postgresql exists on the vendor server but I tried to do a select statement using:
SELECT to_char(sle.log_field1, 'YYYY-MM-DD')
FROM student_log_entires sle;
But I'm getting this error:
Error: SQL Error: SQLSTATE[42883]: Undefined function: 7 ERROR: function to_char(text, unknown) does not exist LINE 25: AND to_char(sle.log_field1, 'YYYY-MM-DD') >=... ^ HINT: No function matches the given name and argument types. You might need to add explicit type casts.
I did also try:
SELECT to_date(sle.log_field1, 'YYYY-MM-DD')
FROM student_log_entries sle
But I got this error:
Error: SQL Error: SQLSTATE[22007]: Invalid datetime format: 7 ERROR: invalid value "[E] " for "YYYY" DETAIL: Value must be an integer. Query: SELECT to_date(sle.log_field1, 'YYYY-MM-DD') FROM student_log_entries sle
Any suggestions/direction would be appreciated. Thanks.
This assumes that lc_time is set to English:
SELECT to_char(to_date('26-AUG-2015', 'DD-MON-YYYY'), 'YYYY-MM-DD');
to_char
------------
2015-08-26
(1 row)
You can convert the value to a date and then back to a string:
select to_char(to_date('26-AUG-2015', 'DD-MON-YYYY'), 'YYYY-MM-DD')
Under many circumstances, processing the value as a date is probably sufficient.
Your approach doesn't work because the value is apparently already stored as a string, so converting it back to a string with a date format doesn't make sense.
EDIT:
You may be able to get by using a simple regular expression:
select (case when col ~ '^[0-9]{2}[-][A-Z]{3}[-][0-9]{4}$'
then to_char(to_date('26-AUG-2015', 'DD-MON-YYYY'), 'YYYY-MM-DD')
end)
Of course, if the formatting errors are more subtle, then a more complex regular expression would be needed.
try this,
SELECT to_char(sle.log_field1, 'YYYY-MM-DD') FROM student_log_entires sale;
In Amazon Redshift I'm looking to convert the current timestamp to have 0 seconds. That is go from this:
2013-12-17 12:27:50
to this:
2013-12-17 12:27:00
I have tried the following:
SELECT dateadd(second, -(date_part(second, getdate())), getdate());
ERROR: function pg_catalog.date_add("unknown", double precision, timestamp without time zone) does not exist
HINT: No function matches the given name and argument types. You may need to add explicit type casts.
SELECT dateadd(second, -cast(date_part(second, getdate()) as double precision), getdate());
ERROR: function pg_catalog.date_add("unknown", double precision, timestamp without time zone) does not exist
HINT: No function matches the given name and argument types. You may need to add explicit type casts.
SELECT getdate() - date_part(second, getdate());
ERROR: operator does not exist: timestamp without time zone - double precision
HINT: No operator matches the given name and argument type(s). You may need to add explicit type casts.
I'm probably missing a very simple way of doing this! Does anyone have any suggestions, please?
It's easiest to use the date_trunc() function, but that will work only while selecting:
SELECT date_trunc('minute', TIMESTAMP '2013-12-17 12:27:00');
You may preprocess data before loading data into the redshift DB, or use intermediary table and then use INSERT INTO...SELECT statement:
INSERT INTO destination_table (
SELECT date_trunc('minute', date_column), other_columns_here
FROM source_table
);
Check date_trunc()
SELECT date_trunc('minute', TIMESTAMP '2013-12-17 12:27:00');
You could format the date with:
select to_char(now(), 'YYYY-MM-DD HH24:MI:00');