Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
410 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 Answer

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...