【问题标题】:Get date from string Kusto从字符串 Kusto 获取日期
【发布时间】:2021-11-02 06:03:40
【问题描述】:

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

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

虽然这有效?

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

如何从“2021 年 7 月”之类的字符串中获取该月的第一天?

【问题讨论】:

    标签: azure-data-explorer kql


    【解决方案1】:

    您看到这种差异的原因是,查询字符串解析器正在使用支持这种格式的 .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

    【讨论】:

      猜你喜欢
      • 2020-03-27
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 2012-04-17
      相关资源
      最近更新 更多