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

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

Related

How to convert string YYYYMM to date in hive 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.

How to convert string to date in pyspark

I have a file name from which I'm extracting date: some_file_name_20201103114823.csv using substring:
substring(input_file_name(),16,8)
I extracted date part, which is now string: 20201103
How can I convert this string to date in format: MM-dd-yyyy ?
This is what i did, formatted string and then cast it to date - not pretty but does the work:
to_date(concat(substring(input_file_name(),16,4),lit("-"),substring(input_file_name(),20,2),lit("-"),substring(input_file_name(),22,2)),"yyyy-MM-dd")
Reference: https://sparkbyexamples.com/pyspark/pyspark-substring-from-a-column/#:~:text=In%20PySpark%2C%20the%20substring(),using%20substring()%20from%20pyspark.

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 convert string with a specific date format like 'mm/dd/yy' to date object in hibernate query

One of the columns my_column in my table is of type varchar where i store value of date in string. The format is 'MM/DD/YY'. I want to use cast function of hibernate to convert this column value to date. But it appears that the cast function accepts string of 'yy-mm-dd' format to convert. Is it possible to use cast for string with specific date format for conversion. Something like this: cast(am.my_column as date format 'mm/dd/yy')
cast function in hibernate is used this way: cast(am.my_column as date).
My use case is basically converting the column value from string to date and making a date comparison. cast(am.my_column as date format 'mm/dd/yy') > '12/30/2017'
Instead of casting a column value inside the query, you may consider to give '12/30/2017' as parameter (with type of Java Date). So that you can directly write am.my_column > :parameterDate

how to covert string datatype to date datatype in HIVE?

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