Oracle query DATE field by hour - sql

Trying to convert an informix query to Oracle 11g.
This gives me the integer hour from a datetime stamp.
create_dtim::datetime hour to hour::char(2)::int
The hours are 24hrs format like 0,1,2,3,4....
I'm trying to accomplish the same in Oracle but no luck. I have so far
TRUNC(start_dtim, 'HH24')
but this is giving me a format like this
3/9/2015 7:00:00 AM
TIA

You need to use TO_CHAR to convert the date fields
select TO_CHAR (start_dtim, 'HH24')
from yourtable
/

Related

Subtract time using Oracle PL/SQL

I'm creating a report using BI Publisher. Now, I want is to subtract 4 hours in the date. For example, the date I get in the oracle database is below,
2019-09-23T10:09:34.054+00:00
Now I want it to return in report using sql is,
2019-09-23T06:09:34.054+00:00
How can I do that?
Thanks!
Use INTERVAL?
SELECT ts - INTERVAL '4' HOUR
FROM yourTable;
Demo
If your source data is actually text, and not a bona fide timestamp column, then you may use TO_TIMESTAMP_TZ to first do a conversion:
SELECT
TO_TIMESTAMP_TZ(REPLACE(s, 'T', ' ') 'YYYY-MM-DD HH24:MI:SS.FF3TZH:TZM') AS ts_original,
TO_TIMESTAMP_TZ(REPLACE(s, 'T', ' ') 'YYYY-MM-DD HH24:MI:SS.FF3TZH:TZM') - INTERVAL '4' HOUR AS ts_offset
FROM yourTable;
You just need to subtract a fraction of time. Like this for 4 hours ago:
select sysdate-1/6 from dual;

turnaround time calculation in oracle SQL developer

I am trying to calculate turnaround time in days between 2 dates for each record.
the first date (ORDERDATE), which is a string that I converted to date format
using To_char(to_date) function
the second date is (CURRENT_DATE) which has proper date format.
SELECT
SPECCODE,
SOURCECODE,
SOURCEDESCRIPTION,
**TO_CHAR( TO_DATE (PATCASE.ORDEREDDATE, 'YYYYMMDD'))"ORDER_DATE",
CURRENT_DATE**
FROM ...........
You could use simple substraction:
SELECT
TRUNC(TO_DATE (PATCASE.ORDEREDDATE, 'YYYYMMDD') - SYSDATE) AS days_diff
FROM ...;
DBFiddle Demo

Convert Date datatype in oracle to UTC format using oracle query

I have a date column in this format "03-FEB-16" in a database table.
I want to query the database to get UTC format equivalent of that date value:
This is the UTC format I am looking for
"2015-12-17T14:30:33Z"
Try this:
SELECT TO_CHAR(cast(SYSDATE as timestamp) at time zone 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"') FROM DUAL
Change SYSDATE to say SYSDATE-150 to test different times of the year
I think I found my answer but Please feel free to post alternative ways to do it.
The following query did the trick:
select to_char(sysdate,'YYYY-MM-DD')||'T'||to_char(sysdate,'HH24:MI:SS')||'Z' from dual

Convert UTC to GMT -5 in Oracle

i need convert a date field in UTC to GMT-5, this is my query;
Select
A1.NAME23,
TO_CHAR(A1.TIME_END,'YYYY/MM/DD HH24:MI:SS')TIME_END,
A1.VAR43
From table
i trying this
select to_char(cast(A1.TIME_END at time zone 'GMT-5' as date ),'YYYY/MM/DD HH24:MI:SS') as GMT-5 from table
but dont work
any suggestions
thanks.
If the column is a DATE datatype, then it's simply:
select time_end - 5/24 from table
It must be this one:
A1.TIME_END at time zone '-05:00'

How to get the date and time from timestamp in PostgreSQL select query?

How to get the date and time only up to minutes, not seconds, from timestamp in PostgreSQL. I need date as well as time.
For example:
2000-12-16 12:21:13-05
From this I need
2000-12-16 12:21 (no seconds and milliseconds only date and time in hours and minutes)
From a timestamp with time zone field, say update_time, how do I get date as well as time like above using PostgreSQL select query.
Please help me.
There are plenty of date-time functions available with postgresql:
See the list here
http://www.postgresql.org/docs/9.1/static/functions-datetime.html
e.g.
SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
Result: 16
For formatting you can use these:
http://www.postgresql.org/docs/9.1/static/functions-formatting.html
e.g.
select to_char(current_timestamp, 'YYYY-MM-DD HH24:MI') ...
To get the date from a timestamp (or timestamptz) a simple cast is fastest:
SELECT now()::date
You get the date according to your local time zone either way.
If you want text in a certain format, go with to_char() like #davek provided.
If you want to truncate (round down) the value of a timestamp to a unit of time, use date_trunc():
SELECT date_trunc('minute', now());
This should be enough:
select now()::date, now()::time
, pg_typeof(now()), pg_typeof(now()::date), pg_typeof(now()::time)