Hive table date column value converson - hive

in Hive table value for one column is like 01/12/17.But I need the value in the format as 12-2017(month-year).How to convert it?

Convert the string to a unix_timestamp and output the required format using from_unixtime.
select from_unixtime(unix_timestamp(col_name,'MM/dd/yy'),'MM-yyyy')

Related

Convert date column to character column

One of the columns is present in date datatype. I need to convert that date column in character datatype so that it can be concatenated with another character column.
Right now my date column is present in the following format : 09-JUN-2020.
Please help me in converting this column to character column.This needs to be done sas enterprise guide.
Thank u so much in advance.
You can use PUT() to convert from numeric to character. You need to find the format you want the output to look like and use that as your second parameter. Assuming you want your date to look like 2020-06-02 character this works:
*puts date as 2020-06-02;
newVar1 = put(dateVar, yymmddd10.);
*creates date variable as 02Jun2020;
newVar2 = put(dateVar, date9.);
FYI - You can find the list of formats available here

NULL values in a string to date conversion

I have a table with the following data:
logs.ip logs.fecha logs.metodo
66.249.93.79 19/Nov/2018:03:46:33 GET
All data columns are string and I want to convert logs.fecha into date with the following format: YYYY-MM-dd HH:mm:ss
I try the following query:
SELECT TO_DATE(from_unixtime(UNIX_TIMESTAMP(fecha, 'yyyy-MM-dd'))) FROM logs
Results of the query are NULL in all rows.
How can I make the conversion string to date for all rows? I know I must use ALTER TABLE but I don't know how to do it.
Thanks
The reason you get null is because the format of the input string is different from the input passed to unix_timestamp. The second argument to unix_timestamp should specify the string format of the first argument. In from_unixtime you can specify the output format desired. If nothing is specified, a valid input to from_unixtime returns an output in yyyy-MM-dd format.
The error can be fixed as below.
from_unixtime(unix_timestamp(fecha,'dd/MMM/yyyy:HH:mm:ss'),'yyyy-MM-dd HH:mm:ss')
You just have to tell Oracle the date format you are reading with TO_DATE.
Try:
SELECT TO_DATE(fecha,'DD/MON/YYYY:HH:MI:SS') FROM logs

How to convert a binary date column date into gregorian date format in sql

This i got to convert a single binary date to Gregorian date ,
Select to_char(to_date(sum(2415020+42744),'J'),'MMDDYYYY') Last_arrear_date from dual;
But I'm not able to convert a binary date column date into Gregorean in xyz table
For Example
Suppose i have a table borm,That has account_open_date column that stores data in binary format , I want to convert that account_open_date column data into Gregorian date format. Please let me know how to convert that
While you have to change the entire column, Don't use the sum function.
Try the below SQL command it will work.
select acct_no, to_char(to_date((account_open_date + 2415020), 'J'),'DD-MON-YYYY')
from borm;
To Change Binary to Gregorian
select column1 ,...columnN, to_char(to_date((Column_name + 2415020), 'J'),'DD-MON-YYYY')
from Table_Name
WHERE Condition;
To change Gregorian to Binary:
select column1,columnN, to_char(to_date(Column_Name,'YYYYMMDD'),'J')-2415020
from Table_Name
where condition ;

how to process text timestamp in hive

I have a column in hive table stored as text. The text looks as shown below
2007-01-01T00:00:00+00:00
I am trying to find difference in time between two timestamp value stored as text in the above format.
Suppose we've got an Hive table dateTest with two column date1 string, date2 string
and suppose that table containing a row with this values:
2007-01-01T00:00:00+00:00,2007-02-01T00:00:00+00:00
The dates are in ISO 8601 UTC format, so if you run this query:
select datediff(from_unixtime(unix_timestamp(date2, "yyyy-MM-dd'T'HH:mm:ss")),from_unixtime(unix_timestamp(date1, "yyyy-MM-dd'T'HH:mm:ss"))) as days
from datetest;
the result is 31

Varchar2 to date in Oracle

Can somebody help me with how to convert varchar column into Date data type?
My createDate column has VarChar2 data type in format example 20050923
I would like to convert this column into format example 2005/09/23
ALTER TABLE test ADD (new_create_date DATE);
UPDATE test SET new_create_date=TO_DATE(createDate,'MM/DD/YYYY');
When I run these sql I get format 23-aug-2005. Why?
Internally date is stored as a 7 byte value. Formatting is not something that is stored there. You get formatting when you pull the data out of the table.
Try this.
SELECT TO_CHAR(NEW_CREATE_DATE,'MM/DD/YYYY')
FROM TEST
You could do this:
update test
set new_create_date to_date(createDate, 'YYYYMMDD');
You just need to give to_date() the right format string.
This is because your database NLS_DATE_FORMAT must be in DD-MON-YYYY format, you can check that by using the below mentioned query.
SELECT *
FROM nls_session_parameters
WHERE parameter = 'NLS_DATE_FORMAT';
Also by definition to_date(x,format) converts a string i.e. x to a datetime, the format part is an optional part which indicates the format that is present for the string value x.
You can further use to_char(x,format) function in your case which converts the x datetime to a string as mentioned by the EvilTech.