Use TO_TIMESTAMP_TZ( date_string, format_model )
to convert it to a TIMESTAMP WITH TIMEZONE
data type then you can use AT TIME ZONE 'UTC'
to convert all values to a common time zone (since you are using a DATE
column and this does not store time zone information) and either use CAST( previous_value AS DATE )
to convert it explicitly to a DATE
data type or allow Oracle to perform an implicit cast in the INSERT
statement:
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE date_temp ( date_value DATE );
Query 1:
INSERT INTO DATE_TEMP (
DATE_VALUE
) VALUES(
TO_TIMESTAMP_TZ(
'2018-03-23T00:00:00.000+00:00',
'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM'
) AT TIME ZONE 'UTC'
)
INSERT INTO DATE_TEMP (
DATE_VALUE
) VALUES(
TO_TIMESTAMP_TZ(
'2018-03-23T12:34:56.123+01:00', -- Note: Different time zone
'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM'
) AT TIME ZONE 'UTC'
)
SELECT * FROM DATE_TEMP
Results:
| DATE_VALUE |
|----------------------|
| 2018-03-23T00:00:00Z |
| 2018-03-23T11:34:56Z | -- Note: value has been converted to the UTC time zone
As an aside:
I want to convert into date datatype as - MM-DD-YYYY
A date does not have a format - it is stored internally to the database as 7-bytes (representing year, month, day, hour, minute and second) and it is not until whatever user interface you are using (i.e. SQL/Plus, SQL Developer, Java, etc) tries to display it to you, the user, and converts it into something you would find meaningful (usually a string) that the date has a format.
If you just want it as a date and do not want a time component then you can either use the TRUNC( date_value )
function to truncate your value back to midnight.
Update
Is it possible to insert record from table 1 to table 2. By this way we don't want to pass the value manually.
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE date_temp ( date_value DATE );
CREATE TABLE string_temp ( string_value VARCHAR2(50) );
INSERT INTO string_temp
SELECT '2018-03-23T00:00:00.000+00:00' FROM DUAL UNION ALL
SELECT '2018-03-23T12:34:56.123+01:00' FROM DUAL;
Query 1:
INSERT INTO DATE_TEMP ( DATE_VALUE )
SELECT TO_TIMESTAMP_TZ(
string_value,
'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM'
) AT TIME ZONE 'UTC'
FROM string_temp
SELECT * FROM DATE_TEMP
Results:
| DATE_VALUE |
|----------------------|
| 2018-03-23T00:00:00Z |
| 2018-03-23T11:34:56Z |