Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

kql - Can't parse date from concatenated string

I am about to lose my mind with something so apparently simple. I want to parse a string into a datetime but it keeps returning null.

If I do todatetime(strcat(2020,"-11-7")) it returns null. And if I do todatetime("2020-11-7") it works. I compared both strings and they are equal.

This is the code that I am testing:

let dateStr = strcat(2020,"-11-7");
let dateStr2 = "2020-11-7";
print dateStr == dateStr2 // True
print todatetime(dateStr) == todatetime(dateStr2) // False

Any idea why is this happening and how can I solve it?

question from:https://stackoverflow.com/questions/65853355/cant-parse-date-from-concatenated-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your attempt to convert strcat(2020,"-11-7") to datetime fails because the string that is created is not a supported datetime format. Please see the list of supported formats in the doc.

However, if you try to convert strcat(2020,"-11-07") (note the 07 instead of just 7), then it will produce the desired result.

Converting "2020-11-7" to datetime does work (like you noticed) even though this is not a supported format, for backward compatibility (but it's recommended to refrain from using unsupported formats, in any case).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...