How to convert string to date in pyspark - dataframe

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.

Related

Date format in BigQuery

Edited: Want to convert the format.
I am kinda new in BigQuery, recently I was working on a project. I want to convert above type of format into yyyy/mm/dd format. How will I do that?
You can combine PARSE_DATE with FORMAT_DATE to get the desired output:
SELECT FORMAT_DATE("%Y/%m/%d",PARSE_DATE("%B %d, %Y","June 10, 2014")) AS date_str
PARSE_DATE will parse the provided string into a DATE type value in BQ format (YYYY-MM-DD) and then FORMAT_DATE will format this DATE to a string with the desired output.
In case someone is wondering how to convert the whole column;
FORMAT_DATE("%Y/%m/%d",PARSE_DATE("%B %d, %Y",ColumnName)) as DesiredColumnName

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

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`

not able to convert string date format in vb.net

I'm taking date from my csv file
Dim odateq As String = sData(0).Trim()
I am getting odateq as 9/15/2015
I want to convert this to 15/9/2015. So I wrote code like this
Dim newdate As DateTime = DateTime.ParseExact(odateq, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentCulture)
And I'm getting an error like this :
String was not recognized as a valid DateTime.
any help is very appreciable...thanks
the code you wrote is using the wrong format; the date you have is in the format M/d/yyyy (month and day without leading zero are a guess because you did not specify it).
try with this one:
Dim newdate As DateTime = DateTime.ParseExact(odateq, "M/d/yyyy", System.Globalization.CultureInfo.CurrentCulture)
the format you set in the ParseExact was telling the function to expect a date in the format dd/MM/yyyy like 01/05/2015 but what you have is not in that format.
after you parse the input date, to get a string with format dd/MM/yyyy use:
Dim dateAsText As String = newdate.ToString("dd/MM/yyyy")

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