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
Related
My datetime is saved in database as varchar in "o" format.
Example:
2020-10-08T06:58:54.0000000+02:00
What is the appropriate format for this kind of data?
I want convert it to datetime.
Fix your data model! Do not store dates as strings; this is inefficient (you need to convert the string to a date whenever you want to perform artihmetic operation), and unsafe (you cannot guarantee that the values are valid dates).
As for your question: it depends the datatype you want to convert that string to. Assuming that you want timestamp with time zone, which seems like the most relevant option here (it allows fractional seconds and a time zone):
select to_timestamp_tz(
'2020-10-08T06:58:54.0000000+02:00',
'yyyy-mm-dd"T"hh24:mi:ss.ff tzh:tzm'
) as myts
from dual
This returns this timestamp with time zone:
08-OCT-20 06.58.54.000000000 +02:00
Do NOT naively convert date-time values with a time zone to a date-time and ignore the time zone if you wish comparisons of those date-time-timezone values to still be accurate.
Your value has a time zone so the most natural method of storing the data would be TIMESTAMP WITH TIME ZONE and to convert it you can use:
TO_TIMESTAMP_TZ( your_column, 'YYYY-MM-DD"T"HH24:MI:SS.FF7TZH:TZM' )
However, if you want the value as a DATE data type then this data type does not support time zones and you should convert all the values to a common time zone (typically this would be the UTC time zone) using:
CAST(
TO_TIMESTAMP_TZ( your_column, 'YYYY-MM-DD"T"HH24:MI:SS.FF7TZH:TZM' )
AT TIME ZONE 'UTC'
AS DATE
)
If you do not use a common time zone then you will find that you can compare values and draw incorrect comparisons.
For example:
SELECT CASE
WHEN TO_TIMESTAMP_TZ( '2020-10-08T06:58:54.0000000+00:00', 'YYYY-MM-DD"T"HH24:MI:SS.FF7TZH:TZM' )
< TO_TIMESTAMP_TZ( '2020-10-08T07:58:54.0000000+02:00', 'YYYY-MM-DD"T"HH24:MI:SS.FF7TZH:TZM' )
THEN 1
ELSE 0
END AS comparison
FROM DUAL
and
SELECT CASE
WHEN CAST(
TO_TIMESTAMP_TZ( '2020-10-08T06:58:54.0000000+00:00', 'YYYY-MM-DD"T"HH24:MI:SS.FF7TZH:TZM' )
AT TIME ZONE 'UTC'
AS DATE
)
<
CAST(
TO_TIMESTAMP_TZ( '2020-10-08T07:58:54.0000000+02:00', 'YYYY-MM-DD"T"HH24:MI:SS.FF7TZH:TZM' )
AT TIME ZONE 'UTC'
AS DATE
)
THEN 1
ELSE 0
END AS comparison
FROM DUAL
Both output:
| COMPARISON |
| ---------: |
| 0 |
However, naively using TO_DATE:
SELECT CASE
WHEN TO_DATE( '2020-10-08T06:58:54.0000000+00:00', 'YYYY-MM-DD"T"HH24:MI:SS##############' )
< TO_DATE( '2020-10-08T07:58:54.0000000+02:00', 'YYYY-MM-DD"T"HH24:MI:SS##############' )
THEN 1
ELSE 0
END AS comparison
FROM DUAL
Outputs:
| COMPARISON |
| ---------: |
| 1 |
db<>fiddle here
If your column is varchar2 and dates are stores in the string then you can use TO_DATE to convert it to date as follows:
SQL> SELECT TO_DATE('2020-10-08T06:58:54.0000000+02:00', 'YYYY-MM-DD"T"HH24:MI:SS.#############')
2 FROM DUAL;
TO_DATE('2020-10-08T
--------------------
08-oct-2020 06:58:54
SQL>
# is used to skip the character. I am ignoring the timezone and millisecond part as while converting to Date(&Time) it is not needed.
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
I have a table column which has timestamp data type. How to pass timestamp value to the table column? I am passing to_timestamp('1240','HH:MI:SS') in oracle procedure. In DB table I have a value like 01-AUG-20 12.40.00.000000000 PM . I only want to store timestamp in the DB table. Is there a way? Please guide me on this. Thanks
You appear to be confused about what the values store. A TIMESTAMP data type stores a date and time with year, month, day, hour, minute, second and optional fractional seconds and time zone components; you cannot have a TIMESTAMP with just hour, minute and second components as it will always have year-day components.
If you want to store time then either:
Use an INTERVAL DAY TO SECOND data type (with zero days);
Use a DATE (or, if you want fractional seconds, TIMESTAMP) data type and set the year-day components to a fixed value (or ignore them);
Use a string in a fixed format; or
Store the number of seconds after midnight and use TO_DATE( value, 'SSSSS' ) to convert to a date and then TO_CHAR to format it as needed.
I would say that if you want to add times then use an INTERVAL data type as it will natively support that.
For example:
CREATE TABLE times1 ( value INTERVAL DAY TO SECOND );
INSERT INTO times1 ( value ) VALUES ( INTERVAL '12:40' HOUR TO MINUTE );
SELECT * FROM times1;
Which outputs:
| VALUE |
| :------------------ |
| +00 12:40:00.000000 |
If you want to display times then use a DATE and ignore the year-to-day components as you can easily format the time using TO_CHAR.
For example:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
CREATE TABLE times2 ( value DATE );
INSERT INTO times2 ( value )
-- Fixed date
SELECT DATE '1970-01-01' + INTERVAL '12:40' HOUR TO MINUTE FROM DUAL UNION ALL
-- Today's date
SELECT TRUNC( SYSDATE ) + INTERVAL '12:40' HOUR TO MINUTE FROM DUAL UNION ALL
-- First of current month
SELECT TO_DATE( '12:40', 'HH24:MI' ) FROM DUAL;
SELECT value, TO_CHAR( value, 'HH24:MI' ) FROM times2;
Which outputs:
VALUE | TO_CHAR(VALUE,'HH24:MI')
:------------------ | :-----------------------
1970-01-01 12:40:00 | 12:40
2020-08-05 12:40:00 | 12:40
2020-08-01 12:40:00 | 12:40
db<>fiddle here
I have a date field in the format of yyyy-MM-dd-hh.mm.ss coming from a db2 source.I want to load into hive and convert to timestamp.
How do I achieve it ?
Your source DB has dot in between the hours, minutes and seconds. Hive supports : in between them like: yyyy-MM-dd HH:mm:ss.
Ref: Hive Date Functions
select
cast(
concat(
substr('2015-07-22-09.00.32',1,10), ' ',
substr('2015-07-22-09.00.32',12,2), ':',
substr('2015-07-22-09.00.32',15,2), ':',
substr('2015-07-22-09.00.32',18,2)
) AS TIMESTAMP
)
;
You can use a combination of unix_timestamp and from_unixtime instead of the substr method that you are currently using.
select cast(
from_unixtime(
unix_timestamp('2017-08-31-12:24:48' , 'yyyy-MM-dd-HH:mm:ss')
)
as timestamp
);
+------------------------+--+
| _c0 |
+------------------------+--+
| 2017-08-31 12:24:48.0 |
+------------------------+--+
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 |