How to convert string YYYYMM to date in hive sql - sql

I have column as string in the format "yyyymm" in hive table, and i would like to convert this to date with day as first of each month. for example 202105 string i would like this to be as date "2021-05-01".
Appreciate your help in this matter.

SELECT TO_DATE(CONCAT(SUBSTR('YYYYMM',1,4),'-',SUBSTR('YYYYMM',5,2),'-01'))
You can try using the following, replace "YYYYMM" with your date value string.

Related

How to search in a string for the date between dateFrom and dateTo

I have three parameters, $motivazione, $dateFrom and $dateTo, in my table I have the following string: {"motivation": 17, "dfp": "31/10/2022"}
The query must return the id of the row if the string in the db contains motivazione: $motivazione and dfp: between dateFrom and dateTo,
with the following query I can only get the date contained in the string:
SELECT column, SUBSTRING (column, CHARINDEX ('dfp', column) +6, 10) FROM myTable
but I can't compare it
how can I do?
Thank you
Convert your date string to a SQL Server date: CONVERT(DATE,'31/10/2022', 103).
The format 103 is dd/mm/yyyy. If you don't specify it, the date might be interpreted in the US format mm/dd/yyyy.
Also, if you have a JSON string, use #AnthonyG's advice to extract the date value.
A where clause would be something like this
WHERE CONVERT(JSON_VALUE(Data, '$.dfp'), 103) BETWEEN '2022-01-01' AND '2022-01-31'
Use the year first format for date literals, then you will not have the dd/mm - mm/dd issue.

Convert yyyymm String to yyyy-mm-dd hh:mm:ss date time in athena

I am trying to convert a string column "yyyymm" to a "yyyy-mm-dd hh:mm:ss" datetime column in athena.
How can I do this?
is this what you want?:
select date_parse(strdatecol,'%b-%d-%Y')
from table
date_parse() works perfectly fine in this case. It reads a string value, the format of the string value and then converts that string value to datetime value. In this case, yyyymm was passed as the string and the format was year and month value.
This gave me the result I was looking for:
SELECT date_parse('yyyymm', '%Y%m')

convert string to date in hive

I have this date format in hive: 20180618151752
Make the following query to leave it in the following way:
select
concat(substr(xdr.time_stop,1,8),' ',
substr(xdr.time_stop,9,2),':',
substr(xdr.time_stop,10,2),':',
substr(xdr.time_stop,11,2)) as date
from padl.fraude_vozm_xdr;
20180618 18:17:52
Now, I need to convert that string field to date, how could I do it?.
Thank you
Use from_unixtime and unix_timestamp.
from_unixtime(unix_timestamp('20180618151752','yyyyMMddHHmmss'),'yyyyMMdd HH:mm:ss')

Impala joining and converting string columns to create date

I have a data set which has three columns in string format.
Year: YYYY
Date: DD
Month: A string from 1 to 12.
I need help joining and converting this to a single date as YYYY-MM-DD. Appreciate any help / guidance.
You can use the CONCAT_WS function as shown below and then cast the string to timestamp if you so desire:
SELECT
CAST(CONCAT_WS("-", year_column, month_column, day_column) AS timestamp) AS full_date FROM a_database.a_table

How to change date format in hive?

My table in hive has a filed of date in the format of '2016/06/01'. but i find that it is not in harmory with the format of '2016-06-01'.
They can not compare for instance.
Both of them are string .
So I want to know how to make them in harmory and can compare them. Or on the other hand, how to change the '2016/06/01' to '2016-06-01' so that them can compare.
Many thanks.
To convert date string from one format to another you have to use two date function of hive
unix_timestamp(string date, string pattern) convert time string
with given pattern to unix time stamp (in seconds), return 0 if
fail.
from_unixtime(bigint unixtime[, string format]) converts the
number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a
string representing the timestamp of that moment in the current
system time zone.
Using above two function you can achieve your desired result.
The sample input and output can be seen from below image:
The final query is
select from_unixtime(unix_timestamp('2016/06/01','yyyy/MM/dd'),'yyyy-MM-dd') from table1;
where table1 is the table name present in my hive database.
I hope this help you!!!
Let's say you have a column 'birth_day' in your table which is in your format,
you should use the following query to convert birth_day into the required format.
date_Format(birth_day, 'yyyy-MM-dd')
You can use it in a query in the following way
select * from yourtable
where
date_Format(birth_day, 'yyyy-MM-dd') = '2019-04-16';
Use :
unix_timestamp(DATE_COLUMN, string pattern)
The above command would help convert the date to unix timestamp format which you may format as you want using the Simple Date Function.
Date Function
cast(to_date(from_unixtime(unix_timestamp(yourdate , 'MM-dd-yyyy'))) as date)
here is my solution (for string to real Date type):
select to_date(replace('2000/01/01', '/', '-')) as dt ;
ps:to_date() returns Date type, this feature needs Hive 2.1+; before 2.1, it returns String.
ps2: hive to_date() function or date_format() function , or even cast() function, cannot regonise the 'yyyy/MM/dd' or 'yyyymmdd' format, which I think is so sad, and make me a little crazy.