convert string to date format in azure data factory using set variable

Jamshed Salik 1 Reputation point
2021-12-23T08:42:36.12+00:00

I have string date in set variable "20211222" and i want to convert it into date format like 2021-12-22. And i have use this function
@formatDateTime('20211222', 'yyyy-MM-dd')
But an error occur
In function 'formatDateTime', the value provided for date time string '20211222' was not valid. The datetime string must match ISO 8601 format
Is there any function to convert this string "20211222" into date?

Azure SQL Database
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

1 answer

Sort by: Most helpful
  1. Nandan Hegde 36,146 Reputation points MVP Volunteer Moderator
    2021-12-23T09:21:14.827+00:00

    Hey,
    You can use the below logic :

    @concat(substring(pipeline().parameters.Test,0,4 ),'-',substring(pipeline().parameters.Test,4,2),'-',substring(pipeline().parameters.Test,6,2))  
    

    Using dataflows:

    https://learn.microsoft.com/en-us/answers/questions/135485/azure-data-factory-date-conversion.html

    {  
        "name": "pipeline1",  
        "properties": {  
            "activities": [  
                {  
                    "name": "Set variable1",  
                    "type": "SetVariable",  
                    "dependsOn": [],  
                    "userProperties": [],  
                    "typeProperties": {  
                        "variableName": "Test",  
                        "value": {  
                            "value": "@concat(substring(pipeline().parameters.Test,0,4 ),'-',substring(pipeline().parameters.Test,4,2),'-',substring(pipeline().parameters.Test,6,2))",  
                            "type": "Expression"  
                        }  
                    }  
                }  
            ],  
            "parameters": {  
                "Test": {  
                    "type": "string",  
                    "defaultValue": "20221223"  
                }  
            },  
            "variables": {  
                "Test": {  
                    "type": "String"  
                }  
            },  
            "annotations": []  
        }  
    }  
    

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.