How to split the datetime using KQL

Son 316 Reputation points
2023-11-08T09:30:10.61+00:00

Hi,

I am using a KQL query to retrieve Azure Update Manager patching reports. I am trying to change the format the date column is displayed in to simply YYYY-MM-DD. By default, the time is being displayed as YYYY-MM-DD:T00:00:00.000Z. I need to know how I can split this out. I tried using the split function but that did not allow me to extend the data out as it was expecting datetime.

Here is the KQL:

// Azure Update Management - Patch status
// Click the "Run query" command above to execute the query and see results.
patchinstallationresources
| where type has "softwarepatches" and properties !has "version"
| extend machineName = tostring(split(id, "/", 8)), resourceType = tostring(split(type, "/", 0)), tostring(rgName = split(id, "/", 4)), tostring(RunID = split(id, "/", 10))
| extend prop = parse_json(properties)
| extend lTime = todatetime(prop.lastModifiedDateTime), patchName = tostring(prop.patchName), kbId = tostring(prop.kbId), installationState = tostring(prop.installationState), classifications = tostring(prop.classifications)
| where lTime > ago(30d)
| project lTime, machineName, patchName, kbId, classifications, installationState //, rgName, resourceType, RunID
|where classifications has "" "Security" or classifications has "Critical"
| where installationState has "Installed" or installationState has "Failed"
| order by ['machineName'] asc

You can see at the start of line 8 we are grabbing the time from the JSON properties.

| extend lTime = todatetime(prop.lastModifiedDateTime)

How do we make this work in the required format?

Example of how the data is displayed currently:

datetime

Azure Update Manager
Azure Update Manager
An Azure service to centrally manages updates and compliance at scale.
{count} votes

Answer accepted by question author
  1. SwathiDhanwada-MSFT 19,073 Reputation points Moderator
    2023-11-09T05:15:25+00:00

    Son To change the format of the date column, you can use the format_datetime() function in your KQL query. Here's an example query that formats the date column to display only the date in the YYYY-MM-DD format:

    extend times = format_datetime(todatetime(properties.lastModifiedDateTime),'yyyy-MM-dd')

    patchassessmentresources
    | where type !has "softwarepatches"
    | extend prop = parse_json(properties)
    | extend lastTime = properties.lastModifiedDateTime
    | extend times = format_datetime(todatetime(properties.lastModifiedDateTime),'yyyy-MM-dd')
    | extend updateRollupCount = prop.availablePatchCountByClassification.updateRollup, featurePackCount = prop.availablePatchCountByClassification.featurePack, servicePackCount = prop.availablePatchCountByClassification.servicePack, definitionCount = prop.availablePatchCountByClassification.definition, securityCount = prop.availablePatchCountByClassification.security, criticalCount = prop.availablePatchCountByClassification.critical, updatesCount = prop.availablePatchCountByClassification.updates, toolsCount = prop.availablePatchCountByClassification.tools, otherCount = prop.availablePatchCountByClassification.other, OS = prop.osType
    | project lastTime,times, id, OS, updateRollupCount, featurePackCount, servicePackCount, definitionCount, securityCount, criticalCount, updatesCount, toolsCount, otherCount
    
    
    

    User's image


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.