创建一个powershell脚本。但是.csv中的数据包含字符串";2020-07-05T12:41:52.000Z";如何将我的sql表的字符串转换为datetime。
$database = 'Database'
$server = '192.168.1.1'
$table = 'KS_Invoicing'
$path = '\hmks01c$temp'
$CSVs = get-childitem $path -Filter *.csv
foreach ($c in $CSVs){
Import-CSV $path$c | ForEach-Object {
$_.PSObject.Properties | Foreach-Object {$_.Value = $_.Value.Trim()}
$column1= $_."Column Date"
if ($column1) {
$query= "insert into "+$table+" VALUES ('"+$column1+"')"
Invoke-Sqlcmd -Database $database -ServerInstance $server -Query $query -U ks -Password ks2020
}
}
Move-Item $path$c "\hmks01c$tempArchive"
}
您显示的日期字符串可以直接转换为datetime对象。
[datetime]"2020-07-05T12:41:52.000Z"
Sunday, July 5, 2020 7:41:52 AM