ORACLE: convert VARCHAR to TIMESTAMP/TIMEZONE [duplicate] - sql

This question already has answers here:
Convert String ISO-8601 date to oracle's timestamp datatype
(2 answers)
Closed 6 years ago.
Any suggestions on how I can convert a VARCHAR of format 2016-03-24T11:31:31+0100 into timestamp/time zone
"FIELD_YYY" which was obviously once a timestamp with time zone
is fed to "TABLE_XXX" and I have to use this field from this table for my uses.
(I believe the T marks the start of the time part when timestamp is converted to VARCHAR)
I need to convert this back to timestamp/time zone.
select to_timestamp('2016-03-24T11:31:31+0100', 'yyy-mm-dd hh24:mi:ss') from DUAL
No I can't change the "FIELD_YYY" or "TABLE_XXX", WHY? I just can't, that's life... :-(

Use to_timestamp_tz() function, enclose "T" in double quotes in the format string and you are good to go.
select to_timestamp_tz('2016-03-24T11:31:31+0100'
, 'yyyy-mm-dd"T"hh:mi:sstzhtzm') as res
from dual
result:
RES
--------------------------------------
24-MAR-16 11.31.31.000000000 AM +01:00

You can use to_timestamp_tz
select to_timestamp_tz(your_string, 'YYYY-MM-DD HH24:MI:SS TZH:TZM') from dual;
select to_timestamp_tz('2011-05-12 19:04:41.032645 +01:00',
'YYYY-MM-DD"T"HH24:MI:SS TZH:TZM') from dual;

Related

How to convert VARCHAR (AM/PM) to TIMESTAMP (24 h) in SQL (Teradata v17)

I've tried multiple solutions, but I keep getting errors. I need to create a new column casting VARCHAR to TIMESTAMP that includes AM, PM or -ideally- changes it to 24 hrs format.
VARCHAR format (Start_Date column): 8/3/2022 4:58:49 PM
I found the below solution is some other post, but I'm getting error: 'Format code appears twice'
SELECT itab.*,
TO_TIMESTAMP(Start_Date, 'MM/DD/YYYY HH:MM:SS AM') AS start_TS
FROM db.info_table itab
Please advise.
You have two problems.
MI is the format for minutes, MM is for months (you have it twice, this is why you are getting that error).
Your date/time string has single digit values for month, day, etc. You can use a pretty simple regex for that.
select to_timestamp(regexp_replace('8/3/2022 4:58:49 PM', '\b([0-9])\b', '0\1'), 'MM/DD/YYYY HH:mi:SS AM')
TO_TIMESTAMP returns a TIMESTAMP(6). If you don't want microseconds you can specify the precision using
CAST(RegExp_Replace(start_date, '\b([0-9])\b', '0\1') AS timestamp(0) FORMAT 'MM/DD/YYYYbHH:Mi:SSbT')
All you need is pad day and month in Teradata (as opposed to Oracle etc). m/d/y format has not been implemented.
select '8/3/2022 4:58:49 PM' date1,
to_timestamp(
lpad(strtok(date1,'/',1),2,'0')||'/'||lpad(strtok(date1,'/',2),2,'0')||'/'||strtok(date1,'/',3)
,'mm/dd/yyyy hh24:mi:ss AM'
);

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

oracle sql to convert timestamp to date [duplicate]

This question already has answers here:
Convert timestamp to date in Oracle SQL
(9 answers)
Closed 2 years ago.
i have to convert different timezone string to date. in my table, i have column defined as varchar2. which contains data as below. i need to convert to one time zone(exampple to central time) and extract date from that. i tried different ways but getting invalid month error or not date. ANy suggestions from your end?
1/16/2020 6:28:44 AM -08:00
11/8/2019 3:20:30 AM -05:00
10/25/2019 6:08:21 PM +05:30
I believe you first need to convert it to timestamp with timezone ad then cast it to date:
select CAST(to_timestamp_tz (dat_col, 'MM/DD/YYYY HH12:MI:SS AM TZH:TZM' ) AS DATE)
from test
here is a demo
This will turn your data into specific timezone :
select
to_timestamp_tz(dat_col, 'MM/DD/YYYY HH12:MI:SS AM TZH:TZM')at time zone 'Europe/Moscow'
from test
Then you can cast that :
select
CAST
(to_timestamp_tz(dat_col, 'MM/DD/YYYY HH12:MI:SS AM TZH:TZM')at time zone 'Europe/Moscow'
AS DATE)
from test
Please provide your expected results for more accurate code...

01830. 00000 - "date format picture ends before converting entire input string"

select to_date('13/03/17 05:43:29,000000000 PM -05:00DD/MM/YY HH24:MI:SS') from
irregularities;
How to convert this date to 24-hour format?
You can convert a string to a timestamp with time zone using:
select to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM')
from dual;
If you only want a date data type then you can cast it:
select cast(
to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM')
as date)
from dual;
If you really only want the string version you can convert it back, which you would usually only do for display:
select to_date(
to_timestamp_tz('13/03/17 05:43:29,000000000 PM -05:00',
'DD/MM/RR HH:MI:SS,FF9 AM TZH:TZM'),
'DD/MM/YYYY HH24:MI:SS')
from dual;
If the original string is coming from a table then just replace the text literal with the column name, and dual with your table name. Of course, that assumes the column is actually a string. If it is actually already a timestamp and your client is just displaying it in a way you don't like, you only need theto_char() part.
Read more about these things in the documentation: to_timestamp_tz, format models, cast() and to_char().

How to convert date to datetime in Oracle?

i have a date in oracle with this format DD-MM-YYY and i want to convert it to datetime with this other format DD-MM-YYY HH24:MI how can i proceed?
I've tried this but nothing is working :
to_date(the_date,'DD-MM-YYY HH24:MI')
and also this:
to_date(to_char(date_debut_p),'DD-MM-YYY HH24:MI')
i have a date in oracle with this format DD-MM-YYY and i want to convert it to datetime with this other format DD-MM-YYY HH24:MI
No, you are confused. Oracle does not store dates in the format you see. It is internally stored in 7 bytes with each byte storing different components of the datetime value.
DATE data type always has both date and time elements up to a precision of seconds.
If you want to display, use TO_CHAR with proper FORMAT MODEL.
For example,
SQL> select to_char(sysdate, 'mm/dd/yyyy hh24:mi:ss') from dual;
TO_CHAR(SYSDATE,'MM
-------------------
11/25/2015 22:25:42
Oracle DATE datatype ALWAYS contains (stores) time.
If you want to see it, you can use function TO_CHAR.
If you want to add, for example, 1 hour, you can just use date_debut_p+1/24.
If you want to covert to timestamp, you can do the following:
Select to_timestamp(date_column, 'DD-MM-YYY') from table;
However, if you want in the required format, you can do the following:
Select to_char(to_timestamp(date_column, 'DD-MON-YY'), 'DD-MM-YYY HH24:MI')
from table;
Hope it helps..