【问题标题】:Convert 'double' to 'Date' in Powershell Select-Object在 Powershell Select-Object 中将“double”转换为“Date”
【发布时间】:2021-05-19 02:08:56
【问题描述】:
 Select-Object -Property Distributor,'File Date','Report Date',Country,'Customer Name)

我正在使用上面的 Select-Object 命令从 Excel 工作表中选择数据。当我尝试使用 Write-SQLTableData

将 excel 写入 SQL Server 时,第三列 'Report Date' 出现以下错误
The given value of type double from the data source cannot be converted to type data of the specified data column.

如何在 Select-Object 命令中应用格式/转换?

【问题讨论】:

  • 您的日期格式如何?报告日期的值是多少?
  • 如果不显示 double 值,您可以尝试使用 $date = [datetime]::FromOADate($theDouble) 进行转换
  • @guiwhatsthat 在 Excel 中就像 16.02.2021
  • 16.02.2021 是 Excel 显示,但您在 'Report Date' 中获得的基础 value 显然是 [double] 作为错误消息显示。请回答@guiwhatsthat 的问题,以便我们查看报告日期实际包含的内容

标签: sql-server powershell type-conversion


【解决方案1】:

作为一个例子,你可以如何对一个属性的值做任何你想做的事情:

$ScriptBlock = {$_.'Report Date'} # Do your conversion here
$psobject | Select-Object -Property Distributor,'File Date', @{n="Report Date";e=$ScriptBlock}

目前脚本块只选择你的属性,但你可以在那里转换它,转换取决于你的属性值

【讨论】:

  • 谢谢....它对我来说就像下面一样.. @{l='Report Date';e={If ($_.'Report Date' -as [datetime]){$ _.'报告日期'} else {[DBNULL]::Value}}}
猜你喜欢
  • 2013-04-15
  • 1970-01-01
  • 2023-01-23
  • 2019-01-08
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多