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.
Related
I have a column with a string representing a datetime, where the month name is 3 letters, and there's the time zone info. How do I convert it into a datetime?
"Jul 13 2020 23:05:58 GMT" --> 2020-07-13T23:05:58.000
This will create a table with a single string column, with a single sample record (for the sake of answering your question)
.set Ts <| print s = "Jul 13 2020 23:05:58 GMT"
This will convert each string value in that column/table from string to datetime
Ts
| extend dt = todatetime(s)
relevant docs:
todatetime()
supported datetime formats
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 |
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 ;
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
I have a date value as Fri Feb 15 19:43:05 EST 2013
I could convert it into 2013-02-15 07:43:05 as String.
Now i need to get the date object of this String. I tried using simpleDateFormat in Groovy/Grails but it would just return the original value: Fri Feb 15 07:43:05 EST 2013
The way I am doing it is:
String dateCreated = dateValue.format("yyyy-MM-dd hh:mm:ss")
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //required format
Date newDate = sdf.parse(dateCreated)
Can anyone help me?
If you want dates without Time Zone I suggest you to look the Joda-Time API. There's a plugin for Grails.
Look at LocalDate and LocalDateTime, you can construct them passing separated values (year, month, day, hour, second).
If you have a Date instance, you just need:
Date dateValue = ...
String dateCreated = dateValue.format("yyyy-MM-dd hh:mm:ss")
and dateCreated will contain value like 2013-02-15 07:43:05. You don't need to do anything else