how to covert string datatype to date datatype in HIVE? - sql

I have a date in string format in hive table (like "20121021") How do I convert this into "yyyy-mm-dd" (ex: 2012-10-21 or 2012/10/21)?

You can also use cast():
select cast(substr(col, 10) as date)
At least, this works for the YYYY-MM-DD format. I should also note that in a date context, a string such as YYYY-MM-DD will typically be converted automatically.

You can use TO_DATE(). Try following:
TO_DATE('20121021')
Or
from_unixtime(unix_timestamp('20121021', 'yyyyMMdd'),'yyyy-mm-dd')

Related

How do I cast a timestamp column as a MM/DD/YYYY in BigQuery

I'm trying to cast the results of an entire column in BigQuery in MM/DD/YYYY format
Time_Column
2022-05-26T17:32:41.000Z
2022-05-28T06:34:23.000Z
Results:
We can try to use FORMAT_DATE function to make it, we can refer to this link Supported Format Elements For DATE to use your expected date format from the datetime type.
SELECT FORMAT_DATE("%m/%d/%Y", Time_Column)
FROM T

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')

how to get date string in format dd.MM from datetime in SQL

how to get date string in format dd.MM from datetime in T-SQL.
Example
20.03 from 20.03.2019
20.03.2019 is not a standard date format assuming it is in string format so, you can use left() :
select left('20.03.2019', 5)
Easiest way would be to use the format function.
format('20.03.2019','dd.MM')

Cast String into Date in BigQuery when Date is in the following format: DD-mon-YYYY

I have a string that is a date and it is in this format 'dd-mon-yy' ie:
27-Jan-18
28-Jan-18
How do I convert it into date with this format 'dd/mm/yyyy' using Standard SQL in BigQuery?
If you want to convert dates from the string format to a particular date format, you will first have to convert the string to a date using the PARSE_DATE function in BigQuery and then use the FORMAT_DATE function to convert the date into the desired format. Here is a snippet of code that converts the string of type "DD-Mon-YY" to a date value of the format dd/mm/yyyy:
SELECT FORMAT_DATE("%d/%m/%E4Y", PARSE_DATE("%d-%b-%y", "name_of_column_with_string_dates")) as formatted_dates FROM `projectId.dataset.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.