How to truncate seconds from timestamp in postgres? - sql

I have the following field in my table:
VISIT_DATE TIMESTAMP(0)
And it is holding time like that:
2022-01-13 11:04:15
Could someone tell me is it possible to cut off that seconds? I really don't need them.
I want to hold time in the following format:
2022-01-13 11:04

Either truncate the timestamp by minutes using date_trunc, which will return a timestamp without seconds, or use to_char if it is only about formatting the output:
SELECT date_trunc('minute',VISIT_DATE) FROM t;
SELECT to_char(VISIT_DATE,'yyyy-mm-dd hh24:mi') FROM t;
Demo:
Using date_trunc (result as timestamp):
SELECT date_trunc('minute','2022-01-13 11:04:15'::timestamp);
date_trunc
---------------------
2022-01-13 11:04:00
(1 Zeile)
Using to_char (result as text):
SELECT to_char('2022-01-13 11:04:15'::timestamp,'yyyy-mm-dd hh24:mi');
to_char
------------------
2022-01-13 11:04

Related

Number of records in a period of time PostgreSQL

I have a PostgreSql query, which should make a count of the results that exist between that time frame (between the field "date" and the current time "now"), however the query does nothing but count all the records without applying the filter, does anyone know what I am missing in the query?
This is the query:
SELECT count(*) from table where date between
TO_TIMESTAMP('2022-8-1 12:00:00','YYYY-M-D HH:MI:SS') and now();
Result: 15,480 (all results, does not apply filter "between")
Greetings and thanks
select TO_TIMESTAMP('2022-8-1 12:00:00','YYYY-M-D HH:MI:SS') ;
to_timestamp
------------------------
2022-01-01 00:00:00-08
Per template patterns here Format functionsTable 9.26. Template Patterns for Date/Time Formatting it needs to be:
select TO_TIMESTAMP('2022-08-01 12:00:00','YYYY-MM-DD HH24:MI:SS') ;
to_timestamp
------------------------
2022-08-01 12:00:00-07
Though it would be easier to do something like:
select '2022-8-1 12:00:00'::timestamptz;
timestamptz
------------------------
2022-08-01 12:00:00-07
Ending up with:
SELECT count(*) from table where date between
'2022-8-1 12:00:00'::timestamptz and now();

Hours and minutes between 2 incorrectly formatted datetimes

So i have some timestamps in a DB and i want to get the hours and minutes difference from them
The problem is the timestamp portion is formatted incorrectly where the hour is always 12 and the minutes portion is actually the hours and the seconds is actually the minutes.
Example DB timestamp: 10/1/2020 12:08:52 AM
So in the above example the time is actually 8:52 AM not 12:08 AM
How can i convert this datetime to something i can use in order to calculate the difference in minutes and hours between these 2 oddly formatted timestamps?
My ideal end goal is something that displays the difference in the HH:MM format
EDIT: the timestamps in oracle actually look like below, and in this eaxmple the 12 means nothing and 18 is actually the hours.
Example of what I'm looking for:
01-OCT-20 12.18.44.000000000 AM - 01-OCT-20 12.12.42.000000000 AM
Output: 06:02 . so the timespan would be 6 hours and 2 minutes in this case.
Thanks,
You can turn your string to an Oracle date (resp timestamp) with to_date() (resp to_timestamp()):
to_timestamp(mystring, 'dd/mm/yyyy ss:hh12:mi am')
Then you can use date arithmetics to compute the difference. Substrating timestamps gives you an interval, which is pretty much what you seem to be looking for, so:
to_timestamp(mystring1, 'dd/mm/yyyy ss:hh12:mi am')
- to_timestamp(mystring2, 'dd/mm/yyyy ss:hh12:mi am')
as myinterval
Like so?
(my default date format is 'yyyy-mm-dd hh24:mi:ss' in Oracle ...)
WITH
indata(sdb) AS (
SELECT '10/1/2020 12:08:52 AM' FROM dual
UNION ALL SELECT '10/1/2020 12:08:52 PM' FROM dual
)
SELECT
TO_TIMESTAMP(sdb,'dd/mm/yyyy 12:hh:mi AM') AS ts
FROM indata;
-- out ts
-- out ---------------------
-- out 2020-01-10 08:52:00
-- out 2020-01-10 20:52:00

Oracle timestamp issue to convert date

I am stuck in a data warehousing task where I need to map a column sourced as varchar2 datatype, and a target in TIMESTAMP (6) WITH TIME ZONE datatype.
Data format contained in source: 2019-08-20
I tried using this mapping:
select to_char(TO_TIMESTAMP_TZ('2019-08-20', 'yyyy-mm-dd"T"hh:mi:sstzhtzm'),'yyyy-mm-dd hh:mi:ss AM tzh:tzm') from dual;
which actually works for dual table.
But when the data loads in target this is the output given:
12/1/9999 12:00:00.000000 AM +00:00
Desired and expected output:
2019-08-20 12:00:00 AM +02:00
As you have only precision till date available with you in the varchar2, all other part of the timestamp (hour, minute, second and fraction of second) will be 0.
Try this:
select TO_TIMESTAMP_TZ('2019-08-20', 'yyyy-mm-dd') from dual;
db<>fiddle demo
Cheers!!
Why do you do TO_CHAR if you're inserting into a TIMESTAMP column?
Just insert this:
select TO_TIMESTAMP_TZ('2019-08-20', 'yyyy-mm-dd"T"hh:mi:sstzhtzm') from dual;

PostgreSQL: truncate hour/min/second from a timestamp

I am using the following query to change all date to the Monday of the corresponding week:
select date_trunc('week', join_date) as join_wk from my_table
This query converts 2017-08-23 11:30:02 to 2017-08-21 00:00:00
I am wondering if it is possible to remove the hour/min/secondfrom the output 2017-08-21 00:00:00? i.e. make the output in the format of 2017-08-21
date_trunc returns a timestamp. You could cast it to a date to lose the time part of it:
SELECT DATE_TRUNC('week', join_date)::DATE AS join_wk FROM my_table
-- Here ----------------------------^

Oracle database: getting time from total amount of minutes from the beginning of a day

Given - number of minutes (number oracle type) from the beginning of a day, e.g. 480. Need to get standard oracle time, e.g. - 08:00:00 AM. Is there any good functions to do such operation?
Better use INTERVAL 'minutes' MINUTE to add the number of minutes. Easy to understand.
your_date_time + INTERVAL '480' MINUTE
For example,
SQL> SELECT TRUNC(SYSDATE), TRUNC(SYSDATE) + INTERVAL '480' MINUTE tmstamp FROM dual;
TRUNC(SYSDATE) TMSTAMP
------------------- -------------------
11/19/2015 00:00:00 11/19/2015 08:00:00
In fact, another way which is independent of NLS settings when you have to pass the date as literal. Thus, instead of using TO_DATE, use the ANSI Date literal which uses a fixed format 'YYYY-MM-DD' and is NLS independent.
SQL> SELECT DATE '2015-11-19' curr_date, DATE '2015-11-19' + INTERVAL '480' MINUTE tmstamp
2 FROM dual;
CURR_DATE TMSTAMP
------------------- -------------------
11/19/2015 00:00:00 11/19/2015 08:00:00
UDPATE
Given - number of minutes (number oracle type) from the beginning of a day, e.g. 480
If the minutes value is not static in SQL to be hard-coded, but a PL/SQL variable, then as #AlexPoole mentioned you need to use NUMTODSINTERVAL.
For example,
NUMTODSINTERVAL(480, 'MINUTE')
Having said that,
The Oracle PL/SQL NUMTODSINTERVAL function converts an input number to its specified Interval Day to Second Unit equivalent. The allowed interval units can be DAY, HOUR, MINUTE, or SECOND.
The return type of the function is INTERVAL.
For example,
SQL> SELECT NUMTODSINTERVAL(480, 'MINUTE') intrvl FROM DUAL;
INTRVL
---------------------------------------------------------------------
+000000000 08:00:00.000000000
Just add minutes/1440 to the date, e.g.
select to_date('1.1.2015','dd.mm.yyyy')+480/1440 from dual;