Custom date format in Oracle - oracle

I have dates in VARCHAR column in a table with this format.
"2019-08-13 00:00:00"
And few are in this format
"Wed Feb 20 2019 15:00:58 GMT+0100"
I want all my fields to be in (2).
I cannot have date type for this column. Please help me in converting

You say that this column can't be a date. But if you are storing dates (or, in this case, timestamps with time zones), you really, really want that column to be the proper data type. Otherwise, you're going to be setting yourself up for a world of pain when someone inserts a string in the wrong format.
Assuming all strings of length 19 are in the first format, they all represent valid dates, and you want all of them to end up in the GMT+1 time zone, something like
select case when length(column_name) = 19
then to_char( to_date( column_name, 'yyyy-mm-dd hh24:mi:ss' ),
'Dy Mon DD YYYY HH24:MI:SS' ) || ' GMT+0100'
else column_name
end
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.

Filtering Data between Specified Dates when Date is a Timestamp

I'm trying to filter data between Jan 1st, 2021 and Jan 31st, 2021 in a Hive table. The column containing the date is in timestamp format (yyyy-mm-dd hh-mm-ss). Is there a way to filter just based on year() month and date without inputting the timing? Any examples/sample code would be welcome!
You can use to_date( dt_time timestamp) dt to convert to date and remove time part.
select current_timestamp() , to_date(current_timestamp() )
Select * from table
Where
To_date(input_datetime ) between to_date(from_datetime) and to_date (to_datetime)

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.

Split Date from Oracle Form to DB

I have a table with columns(year,day-month) -date type- in my database.
and a form with a text field for the user to enter a date.
how can I split the entered date to save it on db as following
year day_month
---- ---------
2018 03-04
I tried SUBSTR(TO_CHAR(TO_DATE(block.field)) in a trigger ,
but it didn't work bcz the column type is date, and I tried to add TO_DATE() as outer but the result was
year day_month
---------- ----------
03-04-2018 03-04-2018
How can I do it without changing my columns type?
I'd suggest you NOT to do that. Always store DATE values into DATE datatype columns. ALWAYS.
Later, if you want to present them differently, apply appropriate functions (such as TO_CHAR) to those values and display them any way you want.
In your example, that would be
TO_CHAR(date_column, 'yyyy') year
or
EXTRACT (year from date_column) year
and
TO_CHAR(date_column, 'dd-mm') day_month
[EDIT]
Once again (to repeat what I've said in a comment): the fact that you named columns in the database "year" (whose datatype is DATE) and "day_month" (whose datatype is also DATE) is completely useless.
Right now is (dd.mm.yyyy hh24:mi) 03.04.2018 10:32.
DATE datatype contains both date and time, so - how do you plan to put "2018" into the "year" column? What will you do with its month/day/hour/minutes/seconds component? It can't just "vanish", has to have some value. Is it the first of January at 00:00:00? Or what?
The same goes to your "day_month" column - it'll contain year, as well as hours/minutes/seconds, whether you want it or not.
Let's start with the "year": if you want to extract it from the Form item, that would be TO_CHAR, such as
to_char(:block.some_item, 'yyyy')
which results in a string, '2018'. You can't store it into a DATE datatype column, so you have to apply TO_DATE to it:
to_date(to_char(:block.some_item, 'yyyy'), 'yyyy')
and it will result in 01.04.2018 00:00:00 >>> see? Day, month, hours ... everything is here.
The alternative is to create those columns as VARCHAR2, but that's even worse.
Seriously, don't do that.
Try the following and make the necessary changes in Oracle Forms, substitute block and columns names instead of variables.
DECLARE
p_year VARCHAR2 (8);
p_date VARCHAR2 (8);
BEGIN
SELECT TO_CHAR (SYSDATE, 'YYYY') INTO p_year FROM DUAL;
SELECT TO_CHAR (SYSDATE, 'DD-MM') INTO p_date FROM DUAL;
DBMS_OUTPUT.put_line ('p_year --> ' || p_year || ' p_date --> ' || p_date);
END;
If your column is a DATE type, expect that it will require you to input a date data also.
In your case, you don't need to split a date. For the YEAR column, if the year value only matters to you, then you can use the TRUNC function
:BLK.YEAR_DATE_FIELD := TRUNC(:BLK.DATE_VALUE, 'YYYY');
and for the MONTH column, just save the date value there.
:BLK.MONTH_DATE_FIELD := :BLK.DATE_VALUE;
Also, maybe you just need to set the format mask of those two fields in Oracle forms. You can set the Format Mask of YEAR field to YYYY and MM-DD to the MONTH field.
The DATE data type is stored dates in tables as 7-bytes
byte 1 - century + 100
byte 2 - (year MOD 100 ) + 100
byte 3 - month
byte 4 - day
byte 5 - hour + 1
byte 6 - minute + 1
byte 7 - seconds+ 1
You CANNOT have a DATE data type that just stores year or month + day; it will always store all the components of the date/time.
So you either store the correct values in each column or you will have to make up values for the components you are not storing and will need to make sure that all the made up values are appropriate for the real values. It is just easier to use the real values in both columns.
So just do:
INSERT INTO your_table(
year,
day_month
) VALUES (
:BLK1.T_DATE,
:BLK1.T_DATE
);
Without splitting the date because a day_month without a year does not make sense (is 29th February a valid date? For the majority of years, no it isn't).
When you want to output it with a format then just format it as a string on output:
SELECT TO_CHAR( year, 'yyyy' ) AS year,
TO_CHAR( day_month, 'dd-mm' ) AS day_month
FROM your_table;

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