convert string to required timestamp format in oracle - sql

From the frontend I am getting a string in this format:
2020-10-15 0:34:51. 751000000
I want to insert it as a timestamp in oracle. But I am unable to insert as I am getting the following error:
SQL Error: ORA-01843: not a valid month
I have checked my nls and database timestamp format is
DD-MM-RR HH12:MI:SSXFF AM
Is there a way to convert the above mentioned string to required timestamp format in oracle?

You seem to want the following format specifier:
'yyyy-mm-dd hh24:mi:ss ff'
You can convert your string to a timestamp with to_timestamp():
select to_timestamp('2020-10-15 0:34:51. 751000000', 'yyyy-mm-dd hh24:mi:ss ff') ts from dual
In this db fiddle, where the timestamp format is set to 'yyyy-mm-dd hh24:mi:ssff', the query returns:
| TS |
| :--------------------------- |
| 2020-10-15 00:34:51751000000 |

Use TO_TIMESTAMP:
INSERT INTO table_name ( value )
VALUES (
TO_TIMESTAMP(
'2020-10-15 0:34:51. 751000000',
'YYYY-MM-DD HH24:MI:SS. FF9'
)
);
Or, use a TIMESTAMP literal:
INSERT INTO table_name ( value )
VALUES ( TIMESTAMP '2020-10-15 0:34:51. 751000000' );
db<>fiddle here

Related

Oracle timestamp string value conversion

I'm Trying to fetch values between two timestamps, however the conversion timestamp failing with formatting error.
SELECT
*
FROM
PKV
WHERE
extended_timestamp BETWEEN TO_TIMESTAMP('28-OCT-22 01.10.37.153016000 PM ASIA/CALCUTTA,DD-MON-YY HH24:MI:SS') AND TO_TIMESTAMP(
'28-OCT-22 10.10.37.153016000 PM ASIA/CALCUTTA,DD-MON-YY HH24:MI:SS')
You put the 2 arguments of TO_TIMESTAMP in only 1 string.
Note also that your date format is NLS dependent.
TO_TIMESTAMP_TZ('28-OCT-22 01.10.37.153016000 PM ASIA/CALCUTTA','DD-MON-YY HH12:MI:SS.FF9 PM TZR', 'NLS_DATE_LANGUAGE = American')
Use a TIMESTAMP literal:
SELECT *
FROM PKV
WHERE extended_timestamp
BETWEEN TIMESTAMP '2022-10-28 13:10:37.153016000 ASIA/CALCUTTA'
AND TIMESTAMP '2022-10-28 22:10:37.153016000 ASIA/CALCUTTA';

Postgres Timestamp to DATE dd-mm-yyyy

im trying to insert select a casted timestamp in a date colum timestamp like 02-09-2021 00:00:00, and i need to convert this timestamp to date dd-mm-yyyy without hhmmss, i've tried select date(column) as newdate but when i check my table insert, it keeps like timestamp, all the solutions that i tried only runs perfectly only in a select sentence, but when i try to insert select.. i keep with timestamp type..
If you need to convert formatted text to timestamp then use to_timestamp with explicit format specification. Try this:
select to_timestamp('02-09-2021 00:00:00', 'dd-mm-yyyy hh24:mi:ss');
to_timestamp
2021-02-09 00:00:00.000 +0200
To convert it to date:
select to_date('02-09-2021 00:00:00', 'dd-mm-yyyy');
If you need to change the type of an existing timestamp column to date then:
alter table the_table alter column the_column type date using the_column::date;

Convert Varchar with timezone to Timestamp in Oracle

I have a problem converting varchar to DateTime/timestamp.
Here is the case
ID EVENT_TIME(Varchar)
1 2020-04-12T09:25:53+0800
2 2020-04-12T09:25:53+0700
3 2020-04-12T09:25:53+0900
return I want, all timestamp convert to +0700
ID EVENT_TIME(Datetime)
1 2020-04-12 10:25:53
2 2020-04-12 09:25:53
3 2020-04-12 11:25:53
is this possible? and how can I do it using oracle?
thanks
Please use below query to convert varchar to timestamp using to_timestamp_tz and again convert it to the required time format using to_char
select ID, to_char(to_timestamp_tz(EVENT_TIME, 'YYYY-MM-DD"T"HH24:MI:SS.FF TZH:TZM'), 'YYYY-MM-DD HH24:MI:SS') as event_date from table_name;
Example:
select to_char(to_timestamp_tz('2020-04-12T09:25:53+0800', 'YYYY-MM-DD"T"HH24:MI:SS.FF TZH:TZM'), 'YYYY-MM-DD HH24:MI:SS') as event_date from dual;
As #Jim said, you can use to_timestamp_tz() with a character literal to convert the string to a timestamp value:
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM')
The to normalise to the +07:00 offset you can use at time zone:
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM') at time zone '+07:00'
If you don't want to keep the time zone part you can cast to a plain timestamp:
cast(
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM') at time zone '+07:00'
as timestamp)
Or you can cast (... as date) as you don't have fractional seconds.
Or you can convert that back to a string for display:
to_char(
to_timestamp_tz(event_time, 'SYYYY-MM-DD"T"HH24:MI:SSTZH:TZM') at time zone '+07:00',
'SYYYY-MM-DD HH24:MI:SS')
db<>fiddle

Date format with UTC

I have to select the value 2019-03-25 from a date column of a table but in the following format:
2019-03-25T00:00:00.000+02:00
Hon can I get it?
Oracle 10g
Thanks!
The date datatype does not store milliseconds and timezone information, so I undertand your question as how to format a date to the target forma, with fixed values for milliseconds and timezone.
If so, you can use to_char() like so:
to_char(mycol, 'yyyy-mm-dd"T"hh24:mi:ss".000+2:00"')
You can CAST your DATE to a TIMESTAMP and then use FROM_TZ to set the time zone and then format it to your requirements using TO_CHAR:
SELECT TO_CHAR(
FROM_TZ( CAST( your_date AS TIMESTAMP ), '+02:00' ),
'YYYY-MM-DD"T"HH24:MI:SS.FF3TZH:TZM'
) AS formatted_date
FROM your_table;
Which, for your sample data:
CREATE TABLE your_table ( your_date ) AS
SELECT DATE '2019-03-25' FROM DUAL
Outputs:
| FORMATTED_DATE |
| :---------------------------- |
| 2019-03-25T00:00:00.000+02:00 |
db<>fiddle here
We can not store timezone information in db that's why we can simply use below format,
select to_char(sysdate,'YYYY-MM-DD')||'T'||to_char(sysdate,'HH24:MI:SS') from dual

Converting string to date with to_date()

I have a table with a VARCHAR(64) column called datetimestamp that contains datetime strings with the following format:
[02/Jun/2016:23:58:30 +0000].
I'm trying to convert this to a date using to_date(datetimestamp, 'DD/Mon/YYYY:HH24:MM:SS') in my select statement, but I'm getting an 'Invalid Format' error. Not sure if its the UTC bit or what that's messing it up... what's the proper syntax?
Thanks!
It is a bit complicated since to_timestamp does not allow time zone information.
I have come up with this query:
WITH d(part) AS
(SELECT regexp_matches(
'02/Jun/2016:23:58:30 +0000',
'^([^ ]*) ([-+]?\d\d)(\d\d)$'
)
)
SELECT
CAST (to_timestamp(d.part[1], 'DD/Mon/YYYY:HH24:MI:SS')
AT TIME ZONE (d.part[2] || ':' || d.part[3])
AS timestamp with time zone)
AS converted
FROM d;
converted
------------------------
2016-06-02 21:58:30+02
(1 row)
(I am at time zone UTC+02.)
select to_date('02/Jun/2016:23:58:30 +0000', 'DD/Mon/YYYY:HH24:MI:SS');
| to_date |
|------------|
| 2016-06-02 |