From a969485fd710534be649e66227fc60569cb7d9e5 Mon Sep 17 00:00:00 2001 From: Gaetano Savo Date: Fri, 6 Jun 2025 14:08:55 +0200 Subject: [PATCH] Gestione import date --- script/importa.ps1 | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/script/importa.ps1 b/script/importa.ps1 index e34558d4..3ad23b7f 100644 --- a/script/importa.ps1 +++ b/script/importa.ps1 @@ -51,17 +51,27 @@ try { $columns = $data[0].PSObject.Properties.Name $columnList = ($columns | ForEach-Object { "[$_]" }) -join "," + # Function to format value for SQL + function Format-SqlValue { + param($value) + if ([string]::IsNullOrEmpty($value)) { + return "NULL" + } + # Try to parse as date + [datetime]$dateValue = [datetime]::MinValue + if ([datetime]::TryParse($value, [ref]$dateValue)) { + return "'" + $dateValue.ToString("yyyy-MM-dd HH:mm:ss") + "'" + } + return "'" + $value.Replace("'", "''") + "'" + } + # Process each row foreach ($row in $data) { # Create value list for this row $values = @() foreach ($column in $columns) { $value = $row.$column - if ([string]::IsNullOrEmpty($value)) { - $values += "NULL" - } else { - $values += "'" + $value.Replace("'", "''") + "'" - } + $values += Format-SqlValue $value } # Create and execute insert query for this row