create oracle date with time stamp from HH24 string - oracle

I have hour in 'HH24' format. For example: '13:35' I need to convert this into today's date of '13:35' in the time stamp - "dd/mm/yyyy HH24:MI:SS".
To_timeStamp() method returning me the date with timestamp of starting of the month.
It is returning "01-Jul-2019 13:35:00", please help me to get today's date with the exact hour and minutes?

You need to concatenate the formatted date with your input and then convert that back to a timestamp:
select to_timestamp(to_char(sysdate, 'yyyy-mm-dd')||' 13:35', 'yyyy-mm-dd hh24:mi')
from dual;
If that time value is a column in your table, use that instead of the constant value:
select to_timestamp(to_char(sysdate, 'yyyy-mm-dd')||' '||the_time_column, 'yyyy-mm-dd hh24:mi')
from your_table;

Related

Convert timezone format in Oracle

I need to convert below timezone format in the following format:
Input:
2020-10-28T20:12:20.986Z
Output:
28-OCT-20 8:12 PM
I tried below query but I am unable to get timestamp with it. Please help.
select TO_TIMESTAMP(SUBSTR('2020-04-21T13:02:31.259Z',1,(INSTR('2020-04-21T13:02:31.259Z', 'T') - 1)),'YYYY-MM-DD HH24:MI:SS') from dual;
One option might be this
SQL> alter session set nls_timestamp_format = 'dd-MON-YY hh:mi PM' ;
Session altered.
SQL> select to_timestamp('2020-10-28T20:12:20.986Z','yyyy-mm-dd"T"hh24:mi:ss.ff3"Z"') from dual ;
TO_TIMESTAMP('2020-10-28T20:12:20.986Z','YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')
---------------------------------------------------------------------------
28-OCT-20 08:12 PM
SQL>
But if you rely better in the to_timestamp function without any session setting, then it is better
SQL> select to_timestamp('2020-10-28T20:12:20.986Z','yyyy-mm-dd"T"hh24:mi:ss.ff3"Z"') from dual ;
TO_TIMESTAMP('2020-10-28T20:12:20.986Z','YYYY-MM-DD"T"HH24:MI:SS.FF3"Z"')
---------------------------------------------------------------------------
28-OCT-20 08.12.20.986000000 PM
You have a timestamp string with a time zone, use TO_TIMESTAMP_TZ rather than TO_TIMESTAMP and then use TO_CHAR to format it:
SELECT TO_CHAR(
TO_TIMESTAMP_TZ(
'2020-04-21T13:02:31.259Z',
'YYYY-MM-DD"T"HH24:MI:SS.FFTZR'
),
'DD-MON-RR HH12:MI AM',
'NLS_DATE_LANGUAGE=American'
)
FROM DUAL;
db<>fiddle here
Note: DATE, TIMESTAMP and TIMESTAMP WITH TIME ZONE are binary data types and are stored in 7-20 bytes (1 byte each for century, year-of-century, month, day, hour, minute and second then up to 6 optional bytes for fractional seconds for TIMESTAMPs and up to 7-bytes for time zone for TIMESTAMP WITH TIME ZONE). It is never stored in any particular format.
How the DATE/TIMESTAMP data types are displayed is dependent on the client application that you are using to query the database; some may use the NLS settings for the user's session but others do not use that. If you want a particular format then convert the DATE/TIMESTAMP to a string using TO_CHAR.

Oracle - how to convert datetime to TZ format?

I have this time format:
2020-09-08 14:00:00
and I want to convert it to following which is from Sydney time to UTC:
2020-09-08T01:00:00Z
First of all, giving the answer considering that your data is actual varchar2:
SQL> SELECT TO_CHAR(CAST(TO_DATE('2020-09-08 14:00:00','YYYY-MM-DD HH24:MI:SS')
2 AS TIMESTAMP) AT TIME ZONE 'Australia/Sydney',
3 'YYYY-MM-DD"T"HH24:MI:SS"Z"')
4 FROM DUAL;
TO_CHAR(CAST(TO_DATE
--------------------
2020-09-08T18:30:00Z
SQL>
Please note that You can also set NLS_TIMESTAMP_TZ_FORMAT = 'YYYY-MM-DD"T"HH24:MI:SS"Z"' in your session (and don't use the TO_CHAR in your query) as per your requirement. Also, you can use UTC timezone instead of Australia/Sydney if you want to convert your local date to UTC timezone. DB<>Fiddle for UTC conversion.
If the given data is already in date datatype stored in your table, then you just need to use CAST as follows: (TO_DATE is not required)
SELECT TO_CHAR(CAST(YOUR_DATE_COLUMN
AS TIMESTAMP) AT TIME ZONE 'Australia/Sydney',
'YYYY-MM-DD"T"HH24:MI:SS"Z"')
FROM YOUR_TABLE;

Fetch Hours from Created date

I'm just trying to fetch Hour of my table from created date in Oracle 12c Database but it is showing error INVALID EXTRACT FIELD FOR EXTRACT FIELD. kindly guide me to fetch hour of my date my code is here...
SELECT
EXTRACT( HOUR FROM (TO_CHAR(CREATED_DATE,'RRRR-MM-DD HH:MI:SS')) ) HOUR
FROM
INVOICE_V;
my Date is stored as 6/1/2020 4:04:50 PM in this format and Extract function is not accept this function.
Do not store dates as strings.
But, since you have, convert it from a string to a date using TO_DATE:
SELECT EXTRACT( HOUR FROM TO_TIMESTAMP(CREATED_DATE,'DD/MM/YYYY HH12:MI:SS AM') ) AS HOUR
FROM INVOICE_V;
If, however, you meant that its just displaying in that format (and is actually a DATE data type) then CAST the date to a timestamp:
SELECT EXTRACT( HOUR FROM CAST( CREATED_DATE AS TIMESTAMP) ) AS HOUR
FROM INVOICE_V;
An hour can not be used in the EXTRACT function.
The only way to extract hour is to use TO_CHAR or subtract it from TRUNC date as follows:
TO_CHAR(created_date,'HH24') -- OR 'HH' as per your requirement
-- OR
FLOOR(24*(created_date- TRUNC(created_date)))
Please note that Oracle does not store dates in any format. It has its own binary representation. What you see while selecting from the table is based on the NLS_DATE_FORMAT parameter.
You can set it according to your requirement.
ALTER SESSION SET NLS_dATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'; -- like this
If you have a date column (or the-like), then:
select extract(hour from cast(created_date as timestamp)) as hr
from invoice_v
Alternatively:
select to_char(created_date, 'hh24') as hr
from invoice_v
The first expression returns an integer number, while the second produces a string.
Note that hour is a language keyword, hence not a good choice for an identifier (here, you used it as a column alias). I changed that.

Querying datetime in oracle

I have a date type column in a table, where I store date along with time.
I want to query it by WHERE clause
I did it this way:
select *
from conference_hall_book
where to_date(end_time,'dd/mon/yyyy hh24:mi:ss') <= to_date('26/oct/2013 15:00:00','dd/mon/yyyy hh24:mi:ss')
But the result has 27/10/2013 8:00:00 AM also in end_time column.
Can any one help me finding the mistake?
The problem occurs because of
to_date(end_time,'dd/mon/yyyy hh24:mi:ss')
This is a wrong usage of the to_date function. To_date converts a string to a date.
When Oracle sees this expression, it will automatically convert the end_time value to a string, using the configured date format of your database/session. This format typically doesn't include the time part, so a date with the value of "27/10/2013 8:00:00 AM" will be converted to the string "27/10/2013" (if your database date format is dd/mm/yyyy).
Your to_date expression will then convert the string value "27/10/2013" back to a date. The resulting date value will be "27/10/2013 00:00:00", so you will have lost the time portion of your original date.
The simple and correct solution is to drop the to_date(end_time) expression and just use end_time. This will also ensure that if you have index on end_time, the query will be able to use that index.
select *
from conference_hall_book
where end_time <= to_date('26/oct/2013 15:00:00','dd/mon/yyyy hh24:mi:ss')

datatype date and time for oracle database

Which type of data type are used to insert date and time in this format
(10-Oct-2013, 04:00 PM) for oracle database..
CREATE TABLE OPERATOR (
LASTPSWDCHANGE DATE,
LASTSIGNONDTTM DATE,
LASTUPDDTTM DATE
);
DATE is the correct type to store date/time values.The DATE data type does not in itself specify any particular format when converting to or from a string.
To convert from string (usually VARCHAR2) to DATE use
TO_DATE(<string with date>, <date format>)
To convert from DATE to VARCHAR2 use
TO_CHAR(<date>, <date format>)
There is a default date format which is determined by the locale of the client. In tools like Toad or SQL developer the default format often doesn't include the time part.
DATE has second precision. For higher precision (millisecond, nanosecond etc) use TIMESTAMP.
EDIT:
You can find documentation on the format specifiers on Oracles website.
In your case, use:
TO_DATE('10-Oct-2013, 04:00 PM', 'DD-MON-YYYY, HH:MI PM')
TIMESTAMP data type can be used here
a TIMESTAMP := TIMESTAMP '2013-10-10 16:00:00';
b TIMESTAMP WITH TIME ZONE := TIMESTAMP '2013-10-10 16:00:00.00 +02:00';
Hope this helps.
You can use DATE as datatype. But you can retrieve date in various format using TO_CHAR function.
An example:
SELECT TO_CHAR( LASTPSWDCHANGE ,'DD-Mon-YYYY, HH:MI AM' ),
TO_CHAR( LASTSIGNONDTTM ,'DD-Mon-YYYY, HH:MI AM' ),
TO_CHAR( LASTUPDDTTM , 'DD-Mon-YYYY, HH:MI AM' )
FROM OPERATOR

Resources