You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本时间戳格式求助:需将hh:mm:ss.xxx改为hh:mm

解决SQL导出时间戳格式(hh:mm:ss.xxx转hh:mm)的问题

有两种简单的方式可以处理这个时间戳格式问题,按需选择即可:

方案1:在SQL查询中直接格式化(推荐)

直接在SELECT语句里对OccurDateTime字段做格式化,导出的CSV直接就是目标格式,无需后续额外处理。使用SQL的CONVERT函数,指定格式代码120后截取前16位,刚好得到yyyy-MM-dd hh:mm的格式:

修改后的sqlcmd命令:

sqlcmd -h-1 -S ZARIRIS\IRIS -d IA3000SDB -Q "SET NOCOUNT ON; Select Distinct TTransactionLog_1.DecisionTimeInterval,
TTransactionLog_1.UserID, CONVERT(varchar(16), TTransactionLog_1.OccurDateTime, 120) as OccurDateTime, TTransactionLog_1.StableTimeInterval

From TTransactionLog_1
Inner join TSystemLog1 On TTransactionLog_1.NodeID=TSystemLog1.NodeID
Inner join TUser On TTransactionLog_1.UserID=Tuser.UserID
where TSystemLog1.NodeID = 3 and TTransactionLog_1.OccurDateTime >= dateadd(HOUR, -1, getdate())" -s  "," -W -o  "C:\atr\karen\adminreport3.csv"

方案2:在PowerShell中用正则替换

如果不想改动SQL查询,可在现有的PowerShell处理管道中新增正则替换步骤,剔除时间戳里的:ss.xxx部分:

修改后的PowerShell命令:

Get-Content "C:\ATR\Karen\adminreport3.csv" | 
ForEach-Object {$_ -replace "44444444","IN PUNCH"} | 
ForEach-Object {$_ -replace "11111111","OUT PUNCH"} |
ForEach-Object {$_ -replace '(\d{2}:\d{2}):\d{2}\.\d{3}', '$1'} |
Set-Content "C:\ATR\Karen\punchreport1.csv" -Force

两种方法最终都会输出符合要求的结果:

112213,2022-10-31 12:08,OUT PUNCH

内容的提问来源于stack exchange,提问作者user2414682

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 00:25:23