How to convert datetime in string format in hadoop to short date? - hadoop

I'm using the following the query to get the date out of string (20191101154559) in Hadoop(hive).
select max(cast(to_date(from_unixtime(unix_timestamp(substr(column3,1,8), 'yyyymmdd'))) as date)) as dt from databasea.table_name_b;
The output i'm getting after running the above script is '31/01/2019' where as the table holds dates of nov 2019. Not sure where i'm going wrong here.
Appreciate if anyone can look into this issue.

from_unixtime you can use it to get in 'yyyymmdd' or 'yyyy-MM-dd'
whatever format you want data in.
For Eg - select from_unixtime(unix_timestamp(substr('20191101154559',1,8), 'yyyymmdd'),'yyyymmdd');
This will return 20191101

Related

How to load files using SQLLDR with date format as yyyymmddhhmmss?

I need to load a table with a .csv file which contains date "20140825145416".
I have tried using (DT date "yyyymmdd hh24:mm:ss") in my control file.
It throws an error as ORA-01821: date format not recognized
I require the data in table as "MM/DD/YYYY HH:MM:SS".
Sample data : 20140825145416
thanks in advance.
Well, I would be remiss if I did not point out that the correct answer is to never store dates as VARCHAR2 data, but make it a proper DATE column and load it like this:
DT DATE "YYYYMMDDHH24MISS"
Formatting is done when selecting. It will make your life so much easier if you ever need to use that date in a calculation.
That out of the way, If you have no control over the database and have to store it as a VARCHAR2, first convert to a date, then use to_char to format it before inserting:
DT CHAR "to_char(to_date(:DT, 'YYYYMMDDHH24MISS'), 'MM/DD/YYYY HH24:MI:SS')"
Note 'MI' is used for minutes. You had a typo where you used 'MM' (months) again for minutes.
I know it's already been said in the previous answer, but it's so important, it's worth repeating. Do not store dates as varchars !!
If your DT column is timestamp then this might work
DT CHAR(25) date_format TIMESTAMP mask "yyyymmddhhmiss"
I used something like this in external tables. Maybe this might help
https://docs.oracle.com/cd/B19306_01/server.102/b14215/et_concepts.htm
and
https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:8128892010789

Convert date '11/2/2014' (mm/d/yyyy) into timestamp in hive

I want to convert date '11/2/2014' into timestamp using Hive.
This date is in string format.
I tried many ways but I am getting NULL values.
This is the syntax:
CAST(FROM_UNIXTIME(UNIX_TIMESTAMP(<date_column>,'MM/dd/yyyy')) as TIMESTAMP)
You always need to go to unix_timestamp and then cast from unix timestamp to normal timestamp. It doesn't look great, but it works.
Just to check, it might be that your syntax is right but your format is wrong. Perhaps your format is dd/MM/yyyy? That is the reason why you should give us your attempts to get it work next time.
Alternative using only String functions:
SELECT CAST(
CONCAT(
REGEX_REPLACE(
REGEX_REPLACE( dirty_date
, '^([0-9]*)/([0-9]*)/([0-9]*)$','$3-0$1-0$2')
, '-0([0-9][0-9])','-$1')
, ' 00:00:00')
AS TIMESTAMP)
FROM dirty_input

wrong date format in bi reports oracle

I have the following date which is in varchar2(11) column in database:
select valid_untill from SALES_ORDERS_V where header_id = 7999410;
30-May-2016
Using rtf template and xml source, the report output (PDF) is:
4950-11-19 04:45:49:0
I don't know its equal to "30-May-2016".
Why this is showing this, as I did not do any formating in rtf?
Not familiar with either RTF or XML-Publisher, but whenever you retrieve a date saved in string format, IF you use it as a date in your code and not as a string, you must make sure you retrieve it correctly.
In this case, with your select statement: it shouldn't be select valid-until from... (or is it really misspelled, with two l at the end: valid_until?) If it is meant to be used as a date, it should be
select to_date(valid_until, 'dd-Mon-yyyy') from ...
Really the problem here is that the date is stored as a string and not in the date datatype. Good luck!

Oracle Date Format Conversion Issue

Am at the end of my tether so hoping someone can help me! I'm really new to Oracle, but do have a SQL background which is why I'm finding this so frustrating!
We have a system that runs Oracle at the back end. I've got very limited access to the system and can only write select queries.
I've written a query that gets the data I want but the date format is coming out as mm dd yyyy what I need is dd/mm/yyyy
I ran SELECT sysdate FROM dual and that come back as:
SYSDATE
03 11 2015
So my select statement reads (action_date is the column in question)
Select username, action_date from adminview
I've tried everything I can think of to change the date format including:
to_date(action_date,'dd/mm/yyyy')
to_date(action_date,'dd/mm/yyyy','nls_language=English')
to_date(to_date(action_date,'mm dd yyyy'),'dd/mm/yyyy')
I've also tried to_char along the same lines.
If you want to format a DATE value, use TO_CHAR():
SELECT username, TO_CHAR(action_date, 'DD/MM/YYYY') AS action_date
FROM adminview;
If it's not a DATE value, then you'll want to convert it to a DATE (based on what it currently looks like), then use TO_CHAR() to format.

Oracle Date formatting "2009-02-13T11:46:40+00:00"

I've had some brilliant help before and I'm hoping you can get me out of a hole again.
I've got a date coming in from a web service in this format:
2009-02-13T11:46:40+00:00
which to me looks like standard UTC format.
I need to insert it into an Oracle database, so I'm using to_date() on the insert. Problem is, I cant get a matching formatting string for it and keep getting "ORA-01861: literal does not match format string" errors.
I know its a fairly trivial problem but for some reason I cannot get it to accept the right format string. Any help appreciated.
Thanks :)
Gareth
You can directly convert it to a TIMESTAMP_WITH_TIME_ZONE datatype.
select
to_timestamp_tz('2009-02-13T11:46:40+00:00','YYYY-MM-DD"T"HH24:MI:SSTZH:TZM')
from
dual
TO_TIMESTAMP_TZ('2009-02-13T11:46:40+00:00','YYYY-MM-DD"T"HH24:MI:SSTZH:TZM
---------------------------------------------------------------------------
13-FEB-09 11.46.40.000000000 AM +00:00
(I'm assuming the input string is using a 24-hour clock since there is no AM/PM indicator.)
If you want to convert that to a simple DATE, you can, but it will lose the time zone information.
SELECT CAST(TO_TIMESTAMP_TZ(REPLACE('2009-02-13T11:46:40+00:00', 'T', ''), 'YYYY-MM-DD HH:MI:SS TZH:TZM') AS DATE)
FROM dual
To import date in specified format you can set nls_date_format.
Example:
alter session set nls_date_format='YYYY-MM-DD HH24:MI:SS'
This way your SQL statements can be shorter (no casts). For various mask look at Datetime Format Models

Resources