英文:
Converting MMM DD YYYY HH:mm:ss datetime format to dd-mm-yyyy HH:mm:ss in Azure DataFlow
问题
如何在ADF数据流的"Derived Column"步骤中将日期时间格式从Jun 18 2023 8:55:31 PM
转换为18-06-2023 20:55:31
的格式,使用dd-mm-yyyy HH:mm:ss?
英文:
How to convert date time formats like Jun 18 2023 8:55:31 PM
to 18-06-2023 20:55:31
dd-mm-yyyy HH:mm:ss as a Derived Column step in ADF Dataflow?
答案1
得分: 1
根据此文档,ADF数据流表达式不支持上述日期格式。我尝试使用数据流表达式将上述MMM dd yyyy
格式转换为所需格式,但得到了空值。
因此,作为一种解决方法,您可以尝试使用字符串操作将其转换为所需格式。为此,您需要将源日期列作为投影中的字符串列。
首先,在数据流中创建一个包含以下值的数组参数(MMM-month
对应MM
)。
["Jan-01","Feb-02","Mar-03","Apr-04","May-05","Jun-06","Jul-07","Aug-08","SeptSep-09","Oct-10","Nov-11","Dec-12"]
在派生列转换中使用以下表达式生成输出。
concat(split(date,' ')[2], '-',split(find($arr,instr(#item,split(date,' ')[1])!=0),'-')[2],'-',split(date,' ')[3],' ',iif(instr(date,'PM')==0,split(date,' ')[4],replace(split(date,' ')[4],split(split(date,' ')[4],':')[1],toString(add(12,toInteger(split(split(date,' ')[4],':')[1]))))))
示例:日期为Jun 18 2023 8:55:31 PM
。
- 首先,我们需要日期,因此我正在获取用空格
' '
拆分输入列的第2项,即18
。 - 对于月份,我通过拆分并将值与数组值进行比较以获取
MM
值的方式获取MMM
值。 - 然后添加年份。对于时间,如果它包含
'PM'
,则将小时加12
并替换原始字符串中的小时。如果不包含'PM'
,则添加相同的字符串。
结果:
(或)
如果您想要在没有数组的情况下满足您的要求,可以尝试以下方法:
concat(substring(split(toString(toTimestamp(date, 'MMM dd yyyy hh:mm:ss a')),'-')[3],1,2),dropLeft(replace(toString(toTimestamp(date, 'MMM dd yyyy hh:mm:ss a')),substring(split(toString(toTimestamp(date, 'MMM dd yyyy hh:mm:ss a')),'-')[3],1,2),split(toString(toTimestamp(date, 'MMM dd yyyy hh:mm:ss a')),'-')[1]),4))
英文:
As per this Documentation, the above date format is not supported in ADF dataflow expressions. I have tried to convert the above MMM dd yyyy
format to required format with the dataflow expressions but got null values.
So, as a workaround you can try to convert those to required format using string operations. For this, you need to take your source date column as string column in the projection.
First create an array parameter in dataflow with below values(Month in MMM-month in MM)
.
["Jan-01","Feb-02","Mar-03","Apr-04","May-05","Jun-06","Jul-07","Aug-08","SeptSep-09","Oct-10","Nov-11","Dec-12"]
Use the below expression in derived column transformation to generate the output.
concat(split(date,' ')[2], '-',split(find($arr,instr(#item,split(date,' ')[1])!=0),'-')[2],'-',split(date,' ')[3],' ',iif(instr(date,'PM')==0,split(date,' ')[4],replace(split(date,' ')[4],split(split(date,' ')[4],':')[1],toString(add(12,toInteger(split(split(date,' ')[4],':')[1]))))))
Example: date is Jun 18 2023 8:55:31 PM
.
- First we need is date, So I am getting 2nd item if split of input column with space
' '
i,e.18
. - For month, I am getting the
MMM
values by split and comparing the value with the array values to get theMM
values. - Then add the year. For the time, if it contains
'PM'
, add12
to the hour and replace the hour in the original string. If it does not contains'PM'
, then add the same string.
Result:
(OR)
If you want to achieve your requirement without the array, you can try this:
concat(substring(split(toString(toTimestamp(date, 'MMM dd yyyy hh:mm:ss a')),'-')[3],1,2),dropLeft(replace(toString(toTimestamp(date, 'MMM dd yyyy hh:mm:ss a')),substring(split(toString(toTimestamp(date, 'MMM dd yyyy hh:mm:ss a')),'-')[3],1,2),split(toString(toTimestamp(date, 'MMM dd yyyy hh:mm:ss a')),'-')[1]),4))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论