How do i convert current time stamp to YYYYMMDDHHMMSS fromat in Pyspark SQL and then to string - pyspark

I am trying the below method but it is still failing
spark.sql("select cast(to_date(current_timestamp(),'yyyymmddhhmmss') as varchar(15))")

Related

Convert timestamp string to date time in EST time Pyspark

I need to convert
2023-01-31T14:11:36-05:00 to 2023-01-31 19:11:36
I am able to do this in presto CAST(From_iso8601_timestamp(timestamp) AS timestamp)
need to replicate this in my pyspark job, I would appreciate if we can convert the string to datetime in EST hours.
https://spark.apache.org/docs/3.1.3/api/python/reference/api/pyspark.sql.functions.to_utc_timestamp.html
Doesnt day what to when the format is ‘2018-03-13T06:18:23+00:00’.

How to convert string formatted date to date using pyspark

I am trying to parse a column's string values (e.g., D20200910.T000000) into a date format using pyspark. I tried the following, but the results came back null:
select(to_date('ingest_id','DYYYYMMDD.THHMMSS')

How to generate current_timestamp() without timezone in Pyspark?

I am trying to get the current_timestamp in a column in my dataframe. I am using below code for that.
df_new = df.withColumn('LOAD_DATE_TIME' , F.current_timestamp())
But this code is generating load_date_time in below format when exported to csv file.
2019-11-19T16:59:44.000+05:30
I don't want the timezone part and want the datetime in this below format.
2019-11-19 16:59:44

AWS glue job to map string to date and time format while converting from csv to parquet

while converting from csv to parquet, using AWS glue ETL job following mapped fields in csv read as string to date and time type.
this is the actual csv file
after mapping and converting, date filed is empty and time is concatenated with today's date
How to convert with proper date and time format?
It uses presto datatypes so data should be in correct format
DATE Calendar date (year, month, day).
Example: DATE '2001-08-22'
TIME Time of day (hour, minute, second, millisecond) without a time
zone. Values of this type are parsed and rendered in the session time
zone.
Example: TIME '01:02:03.456'
TIMESTAMP Instant in time that includes the date and time of day
without a time zone. Values of this type are parsed and rendered in
the session time zone.
Example: TIMESTAMP '2001-08-22 03:04:05.321'
You may use:
from pyspark.sql.functions import to_timestamp, to_date, date_format
df = df.withColumn(col, to_timestamp(col, 'dd-MM-yyyy HH:mm'))
df = df.withColumn(col, to_date(col, 'dd-MM-yyyy'))
df = df.withColumn(col, date_format(col, 'HH:mm:ss'))

Date Format Conversion in Hive

I'm very new to sql/hive. At first, I loaded a txt file into hive using:
drop table if exists Tran_data;
create table Tran_data(tran_time string,
resort string, settled double)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n';
Load data local inpath 'C:\Users\me\Documents\transaction_data.txt' into table Tran_Data;
The variable tran_time in the txt file is like this:10-APR-2014 15:01. After loading this Tran_data table, I tried to convert tran_time to a "standard" format so that I can join this table to another table using tran_time as the join key. The date format desired is 'yyyymmdd'. I searched online resources, and found this: unix_timestamp(substr(tran_time,1,11),'dd-MMM-yyyy')
So essentially, I'm doing this: unix_timestamp('10-APR-2014','dd-MMM-yyyy'). However, the output is "NULL".
So my question is: how to convert the date format to a "standard" format, and then further convert it to 'yyyymmdd' format?
from_unixtime(unix_timestamp('20150101' ,'yyyyMMdd'), 'yyyy-MM-dd')
My current Hive Version: Hive 0.12.0-cdh5.1.5
I converted datetime in first column to date in second column using the below hive date functions. Hope this helps!
select inp_dt, from_unixtime(unix_timestamp(substr(inp_dt,0,11),'dd-MMM-yyyy')) as todateformat from table;
inp_dt todateformat
12-Mar-2015 07:24:55 2015-03-12 00:00:00
unix_timestamp function will convert given string date format to unix timestamp in seconds , but not like this format dd-mm-yyyy.
You need to write your own custom udf to convert a given string date to the format that you need as present Hive do not have any predefined functions. We have to_date function to convert a timestamp to date , remaining all unix_timestamp functions won't help your problem.
select from_unixtime(unix_timestamp('01032018' ,'MMddyyyy'), 'yyyyMMdd');
input format: mmddyyyy
01032018
output after query: yyyymmdd
20180103
To help someone in the future:
The following function should work as it worked in my case
to_date(from_unixtime(UNIX_TIMESTAMP('10-APR-2014','dd-MMM-yyyy'))
unix_timestamp('2014-05-01','dd-mmm-yyyy') will work, your input string should be in this format for hive yyyy-mm-dd or yyyy-mm-dd hh:mm:ss
Where as you are trying with '01-MAY-2014' hive won't understand it as a date string