Converting string to date with to_date() - sql

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 |

Related

SQL: Combine columns to specific datetime and cast to UTC

I am using Oracle SQL. I want to convert three columns to datetime in sql.
My data looks like:
DAY (DATE) HOUR (NUMBER) HALFHOUR (NUMBER)
21.04.22 11 22
21.04.22 11 23
21.04.22 12 24
21.04.22 12 25
21.04.22 13 26
21.04.22 13 27
....
I need to combine each row to the following specific format, in one column:
2022-04-21T13:30:00.00Z
Moreover, it should be converted from an utc time where data comes from (like UTC+3) to UTC+0 automatically.
How do I do this? I googled a lot, but cant do it.
Thanks :)
The solution to your problem is:
SELECT T1.*,
TO_CHAR(FROM_TZ(TO_TIMESTAMP(DAY||' '||HOUR||':'||HALFHOUR, 'DD.MM.RR HH24:MI'), 'Europe/Berlin') AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.ff2"Z"') as time_utc
FROM T1;
TO_TIMESTAMP:
TO_TIMESTAMP converts char of CHAR, VARCHAR2, NCHAR, or NVARCHAR2
datatype to a value of TIMESTAMP datatype.
Syntax is:
TO_TIMESTAMP( string1 [, format_mask] ['nlsparam'] )
FROM_TZ:
In Oracle Database, the FROM_TZ() function converts a timestamp value
and a time zone to a TIMESTAMP WITH TIME ZONE value. Pass the
timestamp value and the time zone as two separate arguments, and the
function returns them as a TIMESTAMP WITH TIME ZONE value.
Syntax is :
FROM_TZ(timestamp_value, time_zone_value)
time_zone_value all possible values an be found out using the below query:
SELECT * FROM V$TIMEZONE_NAMES;
The output generated using above two functions is the time in the germany time-zone and is converted to UTC time zone using the " AT TIME ZONE 'UTC' "
Then using TO_CHAR it is converted to the required format.
You can see the working sample example at the below link:
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=b030170a2c5536b4f80f1287bbfe4aca

Convert varchar into datetime in oracle

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.

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

BigQuery Google timezone conversion

When I try running below query I get timestamp in UTC timezone.
select current_timestamp from table;
Can you please help me to convert timestamp to get in EST timezone.
Thanks
SELECT TIMESTAMP(DATETIME(CURRENT_TIMESTAMP), 'America/New_York')
The trick here is in converting TIMESTAMP to DATETIME which is timezone-less to represent timestamp as a just date/time, then convert back to TIMESTAMP but now specifying needed timezone.
Note, BigQuery still will show it as UTC but timestamp value itself will represent value in respective timezone
Try this instead:
SELECT STRING(CURRENT_TIMESTAMP, 'America/New_York') AS current_timestamp
FROM dataset.table
This converts the timestamps to strings using the New York time zone.
For more control you can use FORMAT_TIMESTAMP (https://cloud.google.com/bigquery/docs/reference/standard-sql/timestamp_functions#format_timestamp)
Example:
SELECT FORMAT_TIMESTAMP("%F %T EST", current_timestamp, "America/New_York") AS current_timestamp_EST FROM `dataset.table` LIMIT 1;
Results:
|-------------------------|
| current_timestamp_EST |
|-------------------------|
| 2020-10-23 18:25:17 EST |
|-------------------------|

How to Convert date field in yyyy-MM-dd-hh.mm.ss” to Timestamp field(yyyy-MM-dd hh:mm:ss) in Hive)

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 |
+------------------------+--+