one column has multiple date format in Azure Databricks

Shambhu Rai 1,411 Reputation points
2022-08-10T05:01:32.02+00:00

Hi Expert,,
How to extract Month and year from one column in data brcks

col_date
02/02/2022

Best Regards,

Azure Data Lake Storage
Azure Data Lake Storage
An Azure service that provides an enterprise-wide hyper-scale repository for big data analytic workloads and is integrated with Azure Blob Storage.
1,559 questions
Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
2,514 questions
Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,624 questions
{count} votes

Accepted answer
  1. SSingh-MSFT 16,371 Reputation points Moderator
    2022-08-12T16:15:55.55+00:00

    Hi @Shambhu Rai ,

    Thank you for using Microsoft Q&A platform and posting your question.

    We may also try to retrieve month and year from a date in Azure Databricks using spark sql functions as shown below:

    230747-image.png

    Hope this will help. Please let us know if any further queries.

    ------------------------------

    • Please don't forget to click on 130616-image.png or upvote 130671-image.png button whenever the information provided helps you.
      Original posters help the community find answers faster by identifying the correct answer. Here is how
    • Want a reminder to come back and check responses? Here is how to subscribe to a notification

2 additional answers

Sort by: Most helpful
  1. AnnuKumari-MSFT 34,556 Reputation points Microsoft Employee Moderator
    2022-08-11T05:24:49.337+00:00

    Hi @Shambhu Rai ,

    Thankyou for using Microsoft Q&A platform and thanks for posting your question.

    As I understand your query, you are trying to retrieve month and year from a date in azure databricks. Please let me know if my understanding is incorrect. I tried to resolve your query using synapse notebook. You can implement the same in databricks as well.

    You can use strftime() to convert a datetime object containing current date and time to different string formats.

    from pyspark.sql import SparkSession  
    from notebookutils import mssparkutils  
    from pyspark.sql.functions import *  
    from datetime import datetime  
    var=datetime.now()  
      
    print(var.strftime('%Y/%m/%d'))  
    year = var.strftime("%Y")  
    print("year:", year)  
      
      
    month = var.strftime("%m")  
    print("month:", month)  
      
    day = var.strftime("%d")  
    print("day:", day)  
    

    230281-image.png

    For more details , kindly visit : Retrieve month and year using python

    Hope this will help. Please let us know if any further queries.

    ------------------------------

    • Please don't forget to click on 130616-image.png or upvote 130671-image.png button whenever the information provided helps you.
      Original posters help the community find answers faster by identifying the correct answer. Here is how
    • Want a reminder to come back and check responses? Here is how to subscribe to a notification
    • If you are interested in joining the VM program and help shape the future of Q&A: Here is how you can be part of Q&A Volunteer Moderators

  2. Shambhu Rai 1,411 Reputation points
    2022-08-16T19:50:28.193+00:00

    Any suggestion or updates


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.