How to convert the JSON date with to_date function - oracle11g

How to convert the specific string Aug 6, 2018 3:17:11 PM with the function to_date?
That string is a result of a Java parsing mechanism for JSON. I would like to be able to recover it directly from oracle.
My real problem is much more comprehensive that looks to be. It evolves the APEX_JSON usage, but it is not the question. Here something near of real scenario.

Use the Mon DD, YYYY HH12:MI:SS AM format model:
SQL Fiddle
Query 1:
SELECT TO_DATE(
'Aug 6, 2018 3:17:11 PM',
'Mon DD, YYYY HH12:MI:SS AM'
) As dt
FROM DUAL
Results:
| DT |
|----------------------|
| 2018-08-06T15:17:11Z |

Related

I have Year & Month but need to incorpate month number also

This is my value in the table : FY20 JAN
And i am looking for 'FY20 (M01) JAN'. How can convert like this in Oracle 11g SQL query ?
First you convert your string to a value of DATE type. Anything enclosed in double quotes is somewhat hard coded and TO_DATE function ignores them as long as they match the characters in the input in their specific locations. Here FY are in location (index) 1 and 2.
alter session set nls_date_format = 'yyyy-mm-dd';
select to_date('FY20 JAN', '"FY"yy MON') d from dual;
D
----------
2020-01-01
Then, you apply another function TO_CHAR to the date value we got above to get the desired output.
select to_char(
to_date('FY20 JAN', '"FY"YY MON')
, '"FY"yy "(M"mm")" MON'
) c from dual;
C
-----------------------
FY20 (M01) JAN

How to convert epoch to datetime redshift?

I work in dbeaver. I have a table x.
TABLE x has a column "timestamp"
1464800406459
1464800400452
1464800414056
1464800422854
1464800411797
The result I want:
Wed, 01 Jun 2016 17:00:06.459 GMT
Wed, 01 Jun 2016 17:00:00.452 GMT
Wed, 01 Jun 2016 17:00:14.056 GMT
Wed, 01 Jun 2016 17:00:22.854 GMT
Wed, 01 Jun 2016 17:00:11.797 GMT
I tried redshift query
SELECT FROM_UNIXTIME(x.timestamp) as x_date_time
FROM x
but didn't work.
Error occurred:
Invalid operation: function from_unixtime(character varying) does not exist
I also tried
SELECT DATE_FORMAT(x.timestamp, '%d/%m/%Y') as x_date
FROM x
Error occurred:
Invalid operation: function date_format(character varying, "unknown") does not exist
Is there any wrong with the syntax? Or is there another way to convert to human readable date and time?
Redshift doesn't have the from_unixtime() function. You'll need to use the below SQL query to get the timestamp. It just adds the number of seconds to epoch and return as timestamp.
select timestamp 'epoch' + your_timestamp_column * interval '1 second' AS your_column_alias
from your_table
UDF is going to be pretty slow. Checked execution time for 3 solutions and 1k rows.
The slowest -
-- using UDF from one of the answers
SELECT from_unixtime(column_with_time_in_ms/ 1000)
FROM table_name LIMIT 1000;
Execution time: 00:00:02.348062s
2nd best -
SELECT date_add('ms',column_with_time_in_ms,'1970-01-01')
FROM table_name LIMIT 1000;
Execution time: 00:00:01.112831s
And the fastest -
SELECT TIMESTAMP 'epoch' + column_with_time_in_ms/1000 *INTERVAL '1 second'
FROM table_name LIMIT 1000;
Execution time: 00:00:00.095102s
Execution time calculated from stl_query -
SELECT *
,endtime - starttime
FROM stl_query
WHERE querytxt ilike('%table_name%limit%')
ORDER BY starttime DESC;
The simplest solution is to create from_unixtime() function:
CREATE OR REPLACE FUNCTION from_unixtime(epoch BIGINT)
RETURNS TIMESTAMP AS
'import datetime
return datetime.datetime.fromtimestamp(epoch)
'
LANGUAGE plpythonu IMMUTABLE;
See Redshift documentation on UDF for details
For quick reference, here is the SQL UDF implementation of the from_unixtime function shown above in Python. I've not tested the performance but I imagine it would be similar to the plain SQL version. It's a whole lot easier to write though.
Note: this calculates the number of seconds from the epoch.
CREATE FUNCTION from_unixtime (BIGINT)
  RETURNS TIMESTAMP WITHOUT TIME ZONE
IMMUTABLE
as $$
  SELECT TIMESTAMP 'epoch' + $1 / 1000 * interval '1 second'
$$ LANGUAGE sql;
I used it like this
CAST(DATEADD(S, CONVERT(int,LEFT(column_name, 10)), '1970-01-01')as timestamp) as column_name
SELECT
,task_id
,CAST(DATEADD(S, CONVERT(int,LEFT(SLA, 10)), '1970-01-01')as timestamp) as SLA
FROM my_schema.my_task_table ;

Coversion of IST date format to Oracle sysdate format

How can I to convert from Indian standard time format to oracle date format.
Eg:
Mon May 23 2016 00:00:00 GMT+0530 (India Standard Time)
required format
23-May-16
This parses the input string and returns a date value to you:
select cast(to_timestamp_tz('Mon May 23 2016 00:00:00 GMT+0530', 'Dy Mon DD YYYY HH24:MI:SS "GMT"TZHTZM') as date) as converted_to_date_value
from dual;
This parses the input string to a "timestamp with time zone" value and formats the value back to a string in your desired format:
select to_char(to_timestamp_tz('Mon May 23 2016 00:00:00 GMT+0530', 'Dy Mon DD YYYY HH24:MI:SS "GMT"TZHTZM'), 'DD-Mon-RR') as converted_to_your_format
from dual;
Enjoy!
Footnote: Please note that there's no such thing as "oracle date format" which you refer to. Oracle has its date data type, which can have many many many different display intepretations depending on your client as well as server locale settings.

How to convert timestamp to string in SQLite?

I have table with timestamps in ms stored in it. I want to convert those timestamps in a human readable form.
Here is a sample output of my table:
SELECT date raw, strftime('%d-%m-%Y', (date/1000)) as_string
FROM my_table
+-----------------+--------------+
| raw | as_string |
+-----------------+--------------+
| 1444687200000 | 06-47-3950 |
+-----------------+--------------+
... ... ...
+-----------------+--------------+
As you can see, the date as string is quite strange (06-47-3950).
How can I obtain 12-10-2015?
Try this:
SELECT date raw, strftime('%d-%m-%Y', datetime(date/1000, 'unixepoch')) as_string
FROM my_table
You need to convert timestamp to date before.
You need to convert timestamp to daytime first. There was an answer on one forum. I quote it here.
Here you are: try those queries to see why and how.
select julianday('1899-12-30 00:00:00');
-- that gives 2415018.5 (remember Julian dates start at noon)
select datetime('40660.9454658044', '+2415018 days', '+12 hours', 'localtime');
-- gets you 2011-04-28 00:41:28 (depending on your local
time)

How to convert 2001 based timestamp to datetime in SQLite?

ZPUBLICATIONDATETIME is of type TIMESTAMP.
So when I do this:
SELECT strftime('%d - %m - %Y ', datetime(ZPUBLICATIONDATETIME, 'unixepoch')) FROM ZTNNEWS;
I get 26 - 05 - 1984 instead of 2015. iOS (Core Data) writes datetime on 1 Jan 2001 based. What is the best approach to get the right date conversion?
Shall I just add 31 years to it or is there an alternative to unixepoch to put in there?
Essentially what I am trying to do is to get the records from past two days:
select *
from ZTNNEWS
where DATETIME(ZPUBLICATIONDATETIME) > DATETIME('now', '-2 day')
But because ZPUBLICATIONDATETIME is of type TIMESTAMP rather than Datetime, it doesn't output anything.
Any advice please?
Just had the same problem. Adding the date value to the seconds of 1 Jan 2001 seems to do the job:
SELECT * FROM ztnnews WHERE DATETIME(zpublicationtime + 978307200) > DATETIME('now', '-2 day');
I used ruby to get the seconds of 1 Jan 2001:
$ irb
2.2.3 :001 > require "time"
=> true
2.2.3 :002 > Time.parse( "2001-01-01T00:00:00Z").to_i
=> 978307200

Resources