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');
Related
Recently there is an issue from my side.
In the table in redshift I have 2 columns:
The visit_time, which mentions the start time of the session and in YYYY-MM-DD HH:MM:SS format; Another column which is the time_spent, and it stands for the millesecond that user spends in certain page and currently it is in float(8)
What I want to do is to Add the visit_time with time_spent in second and convert it into YYYY-MM-DD HH:MM:SS, for example 2018-09-20 21:00:55 + 2 = 2018-09-20 21:00:57, so that I can get the visit_end_time. I tried to add it with date add function like this
Select
dateadd(SECOND,CAST (a.timeonpage AS DECIMAL)/1000 ,a.visit_time::date) time_left,
But it returns error: Invalid operation: function pg_catalog.date_add("unknown", double precision, date) does not exist
To do this, I tried to used the dateadd function like this:
Select
dateadd(SECOND,CAST (a.timeonpage AS INTEGER)/1000 ,a.visit_time::date) time_left,
It does not return error, but it returns the start of the date e.g 2018-09-23 00:00:00, which is not sth that I need.
What is the error that I made and how can I solve it?
Many thanks!
The dateadd function does require an integer for the interval. The problem with your second example is that when you cast a.visit_time to a date type it truncates it to the start of that day (removes the time component). Cast it to a timestamp instead:
select dateadd(second, round(a.timeonpage/1000.0)::integer, a.visit_time::timestamp);
select dateadd(second, round(2345/1000.0)::integer, '2018-09-20 21:00:55'::timestamp);
date_add
---------------------
2018-09-20 21:00:57
Also if you are storing visit_time as a string I would recommend making the column type a timestamp.
I have a little query that strips the date from the datetime field but when I try to convert it from GMT to CST it readds the date. Is there a better way to do this?
Location table:
arrival
4-6-2018 12:35:43
SELECT arrival
FROM(
SELECT CONVERT(VARCHAR(8), arrival))
FROM locations
)a
This query will give me this result:
12:35:43
SELECT (DATEADD(hour,-5,arrival))
FROM(
SELECT CONVERT(VARCHAR(8), arrival))
FROM locations
)a
4-6-2018 12:35:43
This query will give readd the date. How can I remove the date and then do the dateadd function without it readding the date
arrival seems to be a DateTime, which always carries a date part. You need a time instead, supported by SQL Server 2008+:
cast(DATEADD(hour,-5,arrival) as time)
To quote from DATEADD (Transact-SQL) - Return Types:
The return data type is the data type of the date argument, except for string literals. The return data type for a string literal is datetime. An error will be raised if the string literal seconds scale is more than three positions (.nnn) or contains the time zone offset part.
Emphasis my own.
As you are passing a string (varchar), then DATEADD is returning a datetime.
Like those in the comments have said, if you use the correct data type (time) this problem goes away:
SELECT DATEADD(HOUR, -5,CONVERT(time,Arrival))
FROM (VALUES('4-6-2018 12:35:43'),('4-6-2018 07:35:43'),('4-6-2018 03:35:43')) V(Arrival)
Probably this is what you are asking for:
SELECT Convert(Varchar(8), DATEADD(hour,-5,arrival), 108)
FROM locations;
Note: This is compatible with SQL server versions that doesn't have Time datatype too.
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
I have a table with a column defined as time CHAR(6) with values like '18:00' which I need to convert from char to time.
I searched here, but didn't succeed.
You can use the :: syntax to cast the value:
SELECT my_column::time
FROM my_table
If the value really is a valid time, you can just cast it:
select '18:00'::time
As said, you could use :: to cast, but you could also use the standard CAST() function:
SELECT CAST(my_column AS time) AS my_column_time
FROM my_table;
This also works in other databases, not just PostgreSQL.
I've looked at many SO questions related to this but I can't seem to get anything to work. I'm not very good with semi complex SQL queries.
I want to get the difference between the current time and a column that is in unix timestamp in hours.
I'm not sure what I'm doing wrong or right for that matter. The goal is to only pull the rows that is less than 24 hours old. If there is a better way or example that works that would be great.
I tried several answers from here Timestamp Difference In Hours for PostgreSQL
I can't get this query to work no matter how many different ways I try it. wc.posted is a bigint store as unix timestamp
SELECT w.wal_id, wc.com_id, w.posted AS post_time, wc.posted AS com_time
FROM wall as w LEFT JOIN wall_comments as wc ON w.wal_id=wc.wal_id
WHERE (EXTRACT(EPOCH FROM wc.posted)) > current_timestamp - interval '24 hours'
Then the Error:
ERROR: function pg_catalog.date_part(unknown, bigint) does not exist
LINE 1: ... wall_comments as wc ON w.wal_id=wc.wal_id WHERE (EXTRACT(EP...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
ERROR: function pg_catalog.date_part(unknown, bigint) 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: 148
Here is a simplified fiddle
From the fine manual:
A single-argument to_timestamp function is also available; it accepts a double precision argument and converts from Unix epoch (seconds since 1970-01-01 00:00:00+00) to timestamp with time zone. (Integer Unix epochs are implicitly cast to double precision.)
So to convert your bigint seconds-since-epoch to a timestampz:
to_timestamp(wc.posted)
Perhaps you're looking for this:
WHERE to_timestamp(wc.posted) > current_timestamp - interval '24 hours'
Try:
SELECT EXTRACT(EPOCH FROM (timestamp_B - timestamo_A))
FROM TableA
Details here: EXTRACT.
Try this out i am sure this will help you
select field_1,field_2,field_3 from schema_name.table_name Where
ROUND(EXTRACT(EPOCH FROM (cast(now() as timestamp) - cast(your_time_field as timestamp)))/60) > 1440;
The error message anounce :
Hint: No function matches the given name and argument types. You might need to add explicit type casts.
Indeed, the simplest solution is to add an explicit timestamp type cast
SELECT w.wal_id, wc.com_id, w.posted AS post_time, wc.posted AS com_time
FROM wall as w LEFT JOIN wall_comments as wc ON w.wal_id=wc.wal_id
WHERE (EXTRACT(EPOCH FROM wc.posted::timestamp)) > current_timestamp - interval '24 hours'
Notice the cast :
wc.posted::timestamp