从字符串 Kusto 获取日期

Get date from string Kusto

谁能告诉我为什么这不起作用?

let x = "July, 2021";
let calculatedTime = replace_string(x,","," 01");
print(todatetime(calculatedTime))

这行得通吗?

let y = todatetime("July 01 2021");
print y

如何从“2021 年 7 月”这样的字符串中获取每月的第一天?

您看到这种差异的原因是查询字符串解析器正在使用支持此格式的 .Net 库解析第二个查询中的常量值,而查询引擎正在评估第一个表达式使用不同的库,这些库支持指定的日期时间格式 here.

这里是这个问题的解决方法,很可能可以写得更简单:

let GetMonthNumber = view(Month:string){
case(
    Month=="January", "01",
    Month=="February", "02",
    Month=="March", "03",
    Month=="April", "04",
    Month=="May", "05",
    Month=="June", "06",
    Month=="July", "07",
    Month=="August", "08",
    Month=="September", "09",
    Month=="October", "10",
    Month=="November", "11",
    Month=="December", "12", 
    "-1" // default case is an error
)};
let x = "July, 2021";
let dateparts = split(x, ",");
let calculatedTime = strcat(replace_string(tostring(dateparts[1]), " ", ""),"-", GetMonthNumber(tostring(dateparts[0])), "-01");
print todatetime(calculatedTime)
print_0
2021-07-01 00:00:00.0000000