How to validate the incoming date that is.. in yyyyMM format and compare it with the current time stamp(In yyyyMMDD format) up to current month. if the incoming date (i.e) month exceeds the current month reject else populate the date field. Use from_unix_time(unix_timestamp) for validating in spark sql.
needed in the case when statement(spark-sql)
for eg:
EDITED:
spark.sql("select case when (length(date)>0 and from_unixtime(unix_timestamp(date,'yyyyMMdd'),'yyyyMMdd') == date and substring(from_unixtime(unix_timestamp(date,'yyyyMMdd'),'yyyyMMdd'),5,6) == month(current_date())) then substring(date,1,8)else null end as date, case when (length(date)>0 and from_unixtime(unix_timestamp(date,'yyyyMMdd'),'yyyyMMdd') == date and substring(from_unixtime(unix_timestamp(date,'yyyyMMdd'),'yyyyMMdd'),5,6) == month(current_date())) then 'Y' else 'DOB: should be present in YYYYMMDD format'end as date_flag from input").show(false)
In the above edited query it returns null when comparing the incoming month with the current month....it should return 'Y' ....
INPUT: 20210801 EXPECTED OUTPUT: 20210801 Y INPUT: 20210301 EXPECTED OUTPUT: NULL Y INPUT: 03091998 EXPECTED OUTPUT: NULL DOB: should be present in YYYYMMDD format
NOTE: comparison is based on month! if it is current month then print date else reject..